2

I couldn't understand why the following behavior happens in my code

First lets check if there exists number that dividable by 5

int count = 1;
while (count < 10) {
  if (count%5 == 0) {

      fprintf(stderr, FIND);
      exit(1);
  } else {
    printf("Not Yet");
    count += 1;
  }

Normally, I'm expecting that it will print "not yet" four times before it print FIND

however, it actually print FIND immediately even first four number doesn't go to the if statement. There is no output of "Not Yet". The output is simply

FIND

Anyway, I try to debug by adding one more line within the if statement.

int count = 1;
while (count < 10) {
  if (count%5 == 0) {
      printf("%d\n", count);
      fprintf(stderr, INVALID_LINE);
      exit(1);
  } else {
    printf("Not Yet");
    count += 1;
  }
}

The output becomes

Not YetNot YetNot YetNot Yet5
FIND

I couldn't understand

First code, why the program execute if-statement even if the condition doesn't satisfied.

Second code, when one print line is added, the code actually behave what I've expected

ElleryL
  • 467
  • 6
  • 15
  • If you want there to be a positional / temporal relationship between the various outputs of your program, then direct them all to the same stream. As it is, you are printing some of the output to `stderr`, and other output to `stdin` (by default). – John Bollinger Sep 25 '17 at 17:35
  • What platform are you working on? and what C library are you using? – m0h4mm4d Sep 25 '17 at 17:44
  • 2
    `stderr` is not buffered by default. While stdout is buffered. This is why stderr output goes to the screen before the stdout output. If you use `return 1` instead if `exit(1)`, you'd get all the stdout prints before the program exits yet they would be still after the stderr message (on the screen). – hesham_EE Sep 25 '17 at 17:48
  • Like @hesham_EE mentioned, `stderr` is unbuffered, so it will print right away while `stdout` is buffered and won't guarantee to print until it is sent the newline character `'\n'` or it gets manually flushed with a call to `fflush`. In the case of your added line of code in the if statement and how it changed the order of the printing, you added the newline character allowing `stdout` to flush. – Christian Gibbons Sep 25 '17 at 18:17

1 Answers1

2

stderr is unbuffered. Anything you write to it goes out immediately. stdout, which printf writes to, is line buffered-- it only goes out when a new line character is encountered.

Rob K
  • 8,326
  • 2
  • 29
  • 33
  • Is there possible way that print error message with stderr() with certain condition? So that when the error occurs; I can have partly message print in the stdout console. – ElleryL Sep 25 '17 at 21:34
  • I'm sorry, I don't understand what you're asking. – Rob K Sep 26 '17 at 01:02
  • sorry for confusion, basically I'm writing a code that reading through a file, and extract some text from the file and print in console line by line; however, sometimes the text I'm trying to extract is not in the file; in this case I want print error massage to stderr(); however, as my program goes on, before it encounter such error, I still want on going extracted message to be printed (so when error occurs, program stops and still part of the text is printed on console); by using stderr(); my program would immediately terminate – ElleryL Sep 26 '17 at 01:09