printf( ... , comma and then followed by fflush

I am sorry to get ones disgrace me on posting C problem in C++ place
but really need help for:

What is printf( ... comma and followed by fflush(stdout); supposed to mean?
1
2

printf("%" PRIu64 "\n", hunt), fflush(stdout);


Why not just
printf("%" PRIu64 "\n", hunt);
Last edited on
flush forces data to be written immediately. sometimes output can be delayed as it is buffered up for efficiency.

I have never seen it this way: I have always seen it as 2 statements:
printf();
fflush(stdout);

the author thinks the flush is needed. This may depend on the compiler, OS, or other factors whether it really is or not. you can take it out if it works for you without it.

For the C comma operator:

https://lmgtfy.com/?q=C+comma

For the fflush function:

https://lmgtfy.com/?q=c+fflush
You will find a lot of old code where people use a comma instead of a compound statement proper. It's a common trap people fall into.

In this case the side effects are k own and sequenced, so the two statements can be joined by comma, but it would be better to just use curly braces, which improve comprehension and reduce maintenance errors

1
2
3
4
5
if (whatever)
{
  printf( stuff, ... );
  fflush( stdout );
}

It isn't as compact, but it is by far more readable.

Hope this helps.
You will find a lot of old code where people use a comma instead of a compound statement proper. It's a common trap people fall into.

Based on things people post here, it seems that the comma is the "read my mind and do what I really wanted" operator. When people can't be bothered to look up the syntax for the operation they actually want to do, they just throw in a comma without having the slightest idea what it actually does, and assume it will somehow magically do what they want it to do.

Last edited on
Topic archived. No new replies allowed.