ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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)
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
#include<stdio.h>
int main(void)
{
int age;
char name[20];
printf("Enter your age: ");
scanf("%d", &age);
// fflush(stdin); //undefined!!!!!
while (getchar() != '\n') continue;
printf("Enter your first name: ");
scanf("%s", name);
while (getchar() != '\n') continue;
printf("Your age is %d\n", age);
printf("Your first name is %s\n", name);
return 0;
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.