MINGW-packages icon indicating copy to clipboard operation
MINGW-packages copied to clipboard

Result of gcc complie does't follow the correct sequence of "printf"

Open chentian4477 opened this issue 3 years ago • 6 comments

Hello, everyone. I found a problem about the wrong sequence of the "printf" when using gcc compiler in Msys2. There are no prints when I call the "printf" function in the "menu" block. In Msys2, using command " ./a.exe ", there are no prints at first, when I input the digits, but the prints appears after I quit the program. In Window10, double click the a.exe, it prints the message, prints again when I input the digits again. It alway print the messages until I quit the program. (correct running) In Debian11.3, complie with the same code, run the result "./a.out", running is correct too, just like in Windows10.

The c codes are from "C Primer Plus (6th Edition)"-chapter 9: Listing 9.9 usehotel.c Listing 9.10 hotel.c Listing 9.11 hotel.h

Running in Msys2(wrong running): Running in Msys2(wrong running)

Running in Debian11 3(correct running): Running in Debian11 3(correct running)

Running in Windows10(correct running): Running in Windows10(correct running)

Something wrong with Msys2 gcc? How to fix it? hotel.zip

chentian4477 avatar May 08 '22 03:05 chentian4477

Aside from mintty, perhaps you might require a fflush(stdout); after your printfs.

Afaik, mintty (the terminal emulator msys2 uses) handles stdout, stdin, and stderr in a bit special way as they are actually just piped "files" or something, so that might be the reason why?

1480c1 avatar May 08 '22 04:05 1480c1

This might be a MinTTY quirk. Try running the program using WinPTY.

brisingraerowing avatar May 14 '22 01:05 brisingraerowing

I confirm this bug on Windows 8.1

Jolyk avatar May 26 '22 09:05 Jolyk

Will the MinTTY fix this problem?

chentian-4477 avatar May 31 '22 06:05 chentian-4477

This might be a MinTTY quirk. Try running the program using WinPTY.

Will the MinTTY fix this problem?

chentian-4477 avatar May 31 '22 06:05 chentian-4477

Maybe try adding fflush(stdout); every time before calling scanf to tell the terminal that the output buffer should be flushed. Otherwise, the behavior of your example is implementation dependent (iiuc).

mmuetzel avatar May 31 '22 07:05 mmuetzel