0

Ok, so I was putting in prints to investigate where my program stopped executing, it seems I simply cannot print to stdout after a certain code line, that code line being

printf(") : %d\n", PORTNUMBER);

Some examples:

printf("test1");
printf(") : %d\n", PORTNUMBER);
printf("test2");

Prints test1 and the portnumber, but does not print test2.

This:

printf(") : %d\n", PORTNUMBER);
fprintf(stdout, "test2");

Prints the portnumber, but does not print test2.

This:

printf(") : %d\n", PORTNUMBER);
fprintf(stderr, "test2");

Prints both the port number and test2.

So my question is, what's going on here, what could be making me unable to write to stdout after the port number print?

Niklas
  • 13,005
  • 23
  • 79
  • 119
user3284549
  • 313
  • 3
  • 9
  • `stdout` may be full of output, so you may not notice the `test2`. Moreover, `stdout` may get blocked somehow, or some file is used instead of `stdout`. – ForceBru Mar 03 '15 at 14:57

1 Answers1

1

It means that stdout, but not stderr, is line-buffered. The C library will buffer your output until it finds a newline, and only then send the complete line to the underlying output.

You'll see that if you replace "test2" with "test2\n", it will print as you expect. You can also do fflush(stdout) to force stdout to flush without a newline.

stderr, on the other hand, is unbuffered, which is why output to it will be seen immediately.

Dolda2000
  • 25,216
  • 4
  • 51
  • 92