Technical Tips, Tricks and Articles

Posts Tagged ‘c

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 , ,