Sorin
Sorin

Reputation: 910

Infinite loop behaviour in C

I have the following code:

#include<stdio.h>
int main(){
    int a = 1, b = 8;
    while(a != b)
    {
        printf("asd");
        fflush(stdout);
    }
    return 0;
}

Clearly, the program never stops. But why is "asd" not printed at all?

EDIT: This is the complete program. There are not any other lines. The first time I used Eclipse and MinGW and it didn't print anything. I tried then with gcc in linux and it was working as expected, even without fflush! So probably this behaviour might be caused by the fact that some compilers optimize the code and modify the infinite loops.

Upvotes: 0

Views: 131

Answers (1)

Carl Norum
Carl Norum

Reputation: 224944

fflush(stdin) is meaningless, and in fact causes undefined behaviour according to the standard - you probably meant fflush(stdout). If you make that change, you'll see output.

Upvotes: 11

Related Questions