Running order issues for printf and scanf under Eclipse
Eclipse Yuto 2011-12-25 662 Browse to review
When writing a C language, the runtime found that scanf than printf first executed, in the Internet to find a bit of information, the original is the output buffer problem. There are two workarounds, first, the buffer is empty, that is, there is no buffer, and second, after each printf execution, the buffer is forced. Here's how:
Add: Setvbuf (stdout,null,_ionbf,0) before printf, and set the buffer to null.
After each sentence printf is added: Fflush (stdout);
The test code is as follows:
1. Added setvbuf (stdout,null,_ionbf,0);
/* * MAIN.C * * Created on:2015 January 29 * Author:qiang */#include <stdio.h>int Main () {int i = 3; SETVBUF (std out,null,_ionbf,0); printf ("hello\n"); scanf ("%i", &i); printf ("You entered%i", i); return 0;}
2 Add fflush (stdout);
/* * MAIN.C * * Created on:2015 January 29 * Author:qiang */#include <stdio.h>int Main () {int i = 3; printf ("Hel Lo\n "); Fflush (stdout); scanf ("%i", &i); printf ("You entered%i\n", i); Fflush (stdout); scanf ("%i", &i); return 0;}
Running order issues for printf and scanf under Eclipse