Technical Tips, Tricks and Articles

A C Code, getting a string using gets();

leave a comment »

last night one of my friend showed me a C code and told me that he was unable to get the string input using gets(); I am posting the code here.

char temp[50];
printf("Please enter your name: ");
printf("%s", temp);

Now, he was very confused that why this simple code is not working. I asked him to put fflush() before calling to gets(). He did it and the code worked fine. Do you get the reason?

The printf does not flush the memory and when you call the gets() it use the buffer characters already in memory buffer thus returning empty string.

Written by ..alee

January 11, 2007 at 4:01 pm

Posted in C, code, programming

Tagged with , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: