fflush fuction in c
hi everyone
i am trying to learn c but i 've encountered something i am learning from a book (so i can't ask a teacher) i use gcc and when i try to use fflush for removing junk characters the code says #include <stdio.h> int main(void) { int age; char name[20]; puts("Enter your age: "); scanf("%d", &age); fflush(stdin); puts("Enter your first name: "); scanf("%s", name); printf("Your age is %d\n", age); printf("Your first name is %s\n", name); return 0; } but it doesn't work could somebody explain me why(simple i am newbie on this subject and a lot other subjects i think) and another question is there a site or could someone supply me with exercises and as last i've seen an intro to pointers but i don't see so much the advantage of it (maybe when i get to advanced use of it) |
I could not find any error. What is it that isn't working?
Read here about fflush: http://www.cplusplus.com/ref/cstdio/fflush.html The best way to learn is, I think, to experiment on your own. Try to accomplish something and see it work. You are already using pointers! The second parameter to scanf is a pointer. :) Pointers are very useful, and not that complicated. |
at the recommended site i compiled it and got this as answer
Enter some words: testing this program The first word you entered is : testing Enter some words: The first word you entered is : this and at mine code Enter your age: 26 and counting Enter your first name: Your age is 26 Your first name is and could there be something wrong with gcc |
hmm.. I don't know. Perhaps flushing stdin isn't defined or standardized. Maybe someone else knows??
In this particular case you can probably get away with this, though. Replace: fflush(stdin); with: while( getchar() != '\n' ); |
i replaced it with
void clear_kb(void) { char junk[255]; gets(junk); } but i want to know why it doesn't work on my system gcc-3.2 |
Im new to C too, but it seems as though fflush only flushes up until 'new space'. I may be wrong.
|
Re: fflush fuction in c
Code:
#include<stdio.h> |
thx for all the replies but i still don't know why fflush()
is it ANSI-standard???? |
It's in the standard but it should be used for flushing standard output:
fflush(stdout); However flushing standard input is undefined as stated in the standard: fflush(stdin); The reason why you need to clear the standard input buffer after you use scanf is due to the fact that scanf leaves the newline character in the standard input buffer. You need to remove the newline or else it will screw up subsequent scanf calls. On the other hand if you used a function like fgets than the newline is not left in the standard input buffer so there is no problem. The fix for scanf is to use: while (getchar != '\n'); to flush the buffer because fflush(stdin) is undefined! It can crash your program or cause some undefined result. |
Both ANSI and POSIX state that the result of an "fflush()" call on a stream where the last action was a "read()" is undefined. The link you posted above may be true for some C compilers, but in general is bum dope. Both gcc and g++ and the generated objects work correctly.
|
Just an additional comment; fflush() is for ANSI C (FILE *) streams. C++ streams have a similar ::flush manipulator that is only defined for output (ostream) streams. C++ also has an ::ignore manipulator for input (istream) streams which does sort of what you expect fflush(stdin) to do.
|
OK , thx for tour replies
i will leave it for the stdin as it is and try the advice you gave yes the book stated that the scanf leaves the rest of the keys in the keyboard buffer and the next you accessed the input from the keyboard(will get unexpected results) but now i'm puzzled why they mentioned it in the book ah well i'll leave it like it is has someone any ideas for exercises for a newbie in c??? thx again for your replies p.s. couldn't i also use the next function or are there any disadvantages void clear_kb(void) { char junk[255]; gets(junk); } |
gets(junk)
is unsafe, use fgets(junk,255,stdin); |
thx again for the advice
|
All times are GMT -5. The time now is 07:27 AM. |