C program output in wrong order Eclipse

后端 未结 4 656
天涯浪人
天涯浪人 2020-11-29 12:36

I have set up Eclipse for c programming on my Windows machine, I have successfully run a \"hello, world\" program. However, when I try to ask for user input and run the prog

相关标签:
4条回答
  • 2020-11-29 13:09

    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.

    0 讨论(0)
  • 2020-11-29 13:13

    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.

    0 讨论(0)
  • 2020-11-29 13:19

    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'.

    0 讨论(0)
  • 2020-11-29 13:24

    Yes, fflush()ing buffers is necessary to keep the console's screen updated ...

    ... but please guys, it's not Eclipse's fault in- and output might get out of sync, but the library's in use!

    0 讨论(0)
提交回复
热议问题