It's UWAweek 48

help2002

This forum is provided to promote discussion amongst students enrolled in CITS2002 Systems Programming.
Please consider offering answers and suggestions to help other students! And if you fix a problem by following a suggestion here, it would be great if other interested students could see a short "Great, fixed it!"  followup message.

Displaying the 6 articles in this topic
Showing 6 of 919 articles.
Currently 4 other people reading this forum.


 UWA week 35 (2nd semester, week 6) ↓
SVG not supported

Login to reply

👍?
helpful
2:35pm Mon 29th Aug, Cohen R.

Hello, I am having a weird error with my print statements where when I do printf("%c", ch); nothing prints on the screen but when I do printf("%c\n", ch); all the characters print a line apart. Do you have any idea why this might occur. I can confirm that this is the only line of code that changes in the entire program and when I went through it with a debugger, the print line does execute. Thank you


SVG not supported

Login to reply

👍x1
helpful
2:39pm Mon 29th Aug, Christopher M.

"Cohen Rafiq" <23*4*9*[email protected]*u*e*t*u*a*e*u*a*> wrote:
> Hello, > I am having a weird error with my print statements where when I do printf("%c", ch); nothing prints on the screen but when I do printf("%c\n", ch); all the characters print a line apart. Do you have any idea why this might occur. I can confirm that this is the only line of code that changes in the entire program and when I went through it with a debugger, the print line does execute.
When using printf() the output is buffered (pending) until you 'flush' it out with a newline character, call fflush(stdout); or when the whole program terminates (and exit() flushes all non-empty output streams). If you want to always immediately flush all output (i.e. it is no longer buffered), add setbuf( stdout, NULL ); very early in main()


SVG not supported

Login to reply

👍?
helpful
4:24pm Mon 29th Aug, Cohen R.

Nevermind, I changed some stuff and now it's working. I still have no idea what happened though. if anyone has any idea, please still let me know.


SVG not supported

Login to reply

👍?
helpful
4:26pm Mon 29th Aug, Christopher M.

"Cohen Rafiq" <23*4*9*[email protected]*u*e*t*u*a*e*u*a*> wrote:
> Nevermind, I changed some stuff and now it's working. I still have no idea what happened though. if anyone has any idea, please still let me know.
Didn't like or understand my reply?


SVG not supported

Login to reply

👍?
helpful
4:27pm Mon 29th Aug, Cohen R.

Aaah! sorry, your message didn't come up. Thank you for the response. I did try fflush and it still didn't work. I will try out setbuf( stdout, NULL ); Thank you


SVG not supported

Login to reply

👍?
helpful
4:55am Tue 30th Aug, Christopher M.

"Cohen Rafiq" <23*4*9*[email protected]*u*e*t*u*a*e*u*a*> wrote:
> Aaah! sorry, your message didn't come up. Thank you for the response. I did try fflush and it still didn't work. I will try out setbuf( stdout, NULL );
I'm surprised that fflush() didn't appear to work for you. The following program works as I'd expect, flushing its output every 4th character.
#include <stdio.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
    for(int i=0 ; i<20 ; ++i) {
        printf("%c ", 'a'+i);
        sleep(1);

        if((i%4) == 3) {
            printf("flushed ");
            fflush(stdout);
        }
    }
    printf("\n");
    return 0;
}

The University of Western Australia

Computer Science and Software Engineering

CRICOS Code: 00126G
Written by [email protected]
Powered by history
Feedback always welcome - it makes our software better!
Last modified  1:17AM Sep 14 2022
Privacy policy