C program output in wrong order Eclipse

Solution 1:

Yeah, Eclipse will buffer a certain amount of output (I don't remember how much off hand) before it will appear in the output window. Eclipse is communicating with the attached process through a pipe which is fully buffered. It won't flush until either fflush() is called or the buffer is full. I found that when debugging with Eclipse, things work best if I put the following near the beginning of my application:

setvbuf(stdout, NULL, _IONBF, 0);

This will cause stdout to flush immediately whenever it is written to. If you want to use that for debugging and turn it off otherwise, you can conditionally compile it:

#ifdef DEBUG
setvbuf(stdout, NULL, _IONBF, 0);
#endif

No need to put fflush() everywhere this way.

Edit

Here's where I found the solution when I first ran into this issue myself.

http://wiki.eclipse.org/CDT/User/FAQ#Eclipse_console_does_not_show_output_on_Windows

Eclipse's console is not a true console or terminal but rather eclipse is communicating with the attached process through a pipe which is fully buffered not line buffered. This is why a newline '\n' does not cause the buffer to be flushed.

Solution 2:

It sounds like Eclipse is buffering the output of your program and not displaying it right away. This indicates that the "run within Eclipse" feature is not intended to run interactive programs.

You could try adding fflush(stdout); after the first printf, but you shouldn't have to do that just to make your program work in a particular environment.

Solution 3:

Try adding fflush(stdout); after the first printf. This has a decent chance of being of help, in case Eclipse does not auto-flush after '\n'.