printf output always before scanf, who can give a solution?
Solution 1:
This is typically because of output buffering: The printf
call is happening as expected (as you verified by looking at the generated assembler) but nothing shows on the screen: It all went to some buffer and is waiting there patiently until some amount of output data has accumulated that's worthwhile to suspend the program and call an operating system routine for. A typical buffer size would be 4096 bytes. You can verify this theory by writing 4096 (or maybe 4097) characters to stdout, or if that doesn't work, the next powers of 2 until something shows.
Side note: On a normal terminal scanf
would automatically cause a flush of stdout exactly to avoid this situation. Maybe Embarcadero (which is a GUI oriented IDE) uses input and output for a console program that is not identifiable as a terminal so that this doesn't work. Out of curiosity you could try to find out where the executable is, open a normal Windows Console and execute your program directly per command line. I'd not be surprised if it worked as expected.
What can you do with your program to make it work properly wit Embarcadero? You can fflush()
stdout each time you want to see output on the screen, or call setbuf()
once and have everything show from then on.
But it is debatable whether you should clutter up your program at all because of a misbehaving development environment. If you can run it successfully as-is through a regular console I'd rather do that.