msys2.github.io icon indicating copy to clipboard operation
msys2.github.io copied to clipboard

MinGW GCC mixing up the order of the lines I write?

Open canoshiz opened this issue 5 years ago • 2 comments

I recently started using MSYS2 so I haven't had a chance to make anything spectacular with it yet, but I've run into a problem with the C compiler (or what I assume is a problem with the C compiler).

Here's an example program I wrote:

#include <stdio.h>
#include <string.h>
char input[100];
int main() {
        for(;;) {
                printf("> ");
                gets(input);
                if ( strcmp(input, "hello") == 0 ) {
                        printf("hi :)\n");
                } else if ( strcmp(input, "goodbye") == 0 ) {
                        printf("bye :)\n");
                        return 0;
                } else {
                        printf("Could you repeat that?\n");
                }
        }
}

The expected output of this would look something like this:

> hello
hi :)
> d
Can you repeat that?
> goodbye
bye :)

However, the output I actually get looks like this:

hello
d
goodbye
> hi :)
> Can you repeat that?
> bye :)

I found the cause of this to be that even though I wrote the printf("> "); line before the gets(input); line, the compiler switches them at build time.

If I build this code on Cygwin or on a *NIX system it runs as expected, so this must be an issue with MSYS2.

Thanks for reading, and I hope this issue can be resolved. :)

canoshiz avatar Apr 04 '21 18:04 canoshiz

After further investigating I've found out that it is specifically the MinGW toolchain that's broken; the C compiler from msys2-devel works as expected.

canoshiz avatar Apr 04 '21 21:04 canoshiz

Did you try fflush(stdout);?

StUser4pda avatar May 26 '21 15:05 StUser4pda