#include
void main(){
float a,b;
double tich;
printf("%s","Nhap so thu nhat: ");
fflush(stdout); //tai sao co them dong nay moi printf truoc?
scanf("%f",&a);
printf("%s","Nhap so thu hai: ");
fflush(stdout); //
scanf("%f",&b);
tich = a * b;
printf("Tich 2 so la: %f",tich);
}
SteffenI believe that the '\n' character with C functions (e.g. printf()) works similarly to C++'s endl (i.e. '\n' will implicitly flush the buffer for> you). C++ gives you a bit more control as you are able to insert new lines without flushing the buffer (if desired).
In your test case, I see output before the scanf()'s when running this directly from the command line. However, I know better than to try that from within Eclipse due to the mentioned bug in one of my previous posts.
I suggest you try running your program directly from the command line (either in a command window if you're using Windows, or a terminal window on *nix/Mac) and see if the output differs from when you run it within Eclipse. If your results are consistent then I must have missed something, otherwise I suggest you get involved the bug that I mentioned as I have a feeling that it effects a lot of people (and is quite annoying, to say the least). If I'm running my program on my Windows console, the output appears as intended. But I don't think that it has something to do with the bug you've found since the flushing doesn't take place in Eclipse 3.1 RC1 and Eclipse 3.1 final release.DaCypher:
First of all, "endl" flushes the output buffer as well as inserting a newline. Doing an explicit flush should have no effect. Are you sure about this? I'm only familiar with the C case, where a n in a printf() statement flushes the output buffer *if the output is going to a terminal*. If the output is going to a file, n does not flush the buffer. I have to ask about this. The following program will never show ANY output, before entering values for scanf, if I don't explicitly flush thestdout.
#include
int x;
double pi;
char c;
char d;
char str[20];
int main(){
x = 5;
pi = 3.1415926535897932;
c = 255;
d = 'a';
printf("%fn", pi);
printf("%dn", c);
printf("%cn", d);
printf("Enter number: ");
//fflush(stdout);
scanf("%d", &x);
printf("Enter string: ");
//fflush(stdout);
scanf("%s", str);
printf("%dn", x);
printf("%sn", str);
return 0;
}
If I flush the stdout with fflush, the output results as intended.--------------
Hey, fflush works... Still don't know exactly how. Maybe that's "another story to be told at another time"...
^_^
Thanks.