1

I just installed Eclipse and MinGW on my sister's computer running Windows 7.
While I was testing to see if everything is set properly, I noticed that this won't print anything:

#include<stdio.h>

int main() {
    int x;

    printf("Hello World!\n");
    printf("Enter a number... \n");
    scanf("%d", &x);
    printf("You entered %d", x);
    return 0;
}

instead, it just waits for input, and print the whole thing at once.
Here's how it looks:

345
Hello World!
Enter a number... 
You entered 345

It is only after I added a call to fflush(stdout) after printf("Enter a number... \n"); that Eclipse printed it in the right order, meaning, this code:

#include<stdio.h>

int main() {
    int x;

    printf("Hello World!\n");
    printf("Enter a number... \n");
    fflush(stdout);
    scanf("%d", &x);
    printf("You entered %d", x);
    return 0;
}

Works exactly as expected.
It is worth mentioning that I opened a separate C++ project and tried the same thing:

#include<iostream>

int main() {
    int x;
    std::cout<<"Hello!\n";
    std::cout<<"Enter a number\n";
    std::cin>>x;
    std::cout<<"you entered "<<x;
}

This worked perfectly fine, and prints without the need of flushing anything...
Why does Eclipse delay the output in C projects?

so.very.tired
  • 2,958
  • 4
  • 41
  • 69

0 Answers0