2

I have the following simple code in C language:

#include <stdio.h>

int main(){
    printf("Give an integer:\n");
    int x;
    scanf("%d",&x);
    printf("10*%d=%d\n",x,10*x);

    return 0;
}

Using CodeBlocks IDE it is executed in the right order but when I use Eclipse IDE it jumps to the scanf command and then prints the messages as it should. Can anyone explain this?

Thank you in advance

Casπian
  • 25
  • 1
  • 3
  • 2
    maybe one environment sets `stdout` to *fully buffered* rather than the usual *line buffered*. Try adding `fflush(stdout);` after `printf()` calls to force outputs. – pmg Apr 29 '20 at 13:37
  • Thank you, it worked. In the CodeBlocks IDE nothing changed, as I wanted, and in the Eclipse it works as i wanted. – Casπian Apr 29 '20 at 13:43
  • Alternatively, see if you can prevent the IDE to set `stdout` to *fully buffered*. – pmg Apr 29 '20 at 13:45

1 Answers1

3

Usually stdout is set to line buffered. Apparently one of your IDEs sets it to fully buffered.

You can force prints to dump the associated buffer with fflush(), eg

printf("hello ");   // works in unbuffered stream
printf("world!\n"); // works in line buffered stream
fflush(stdout);     // works in fully buffered stream
pmg
  • 106,608
  • 13
  • 126
  • 198