Why scanf("%d",&n) and gets(string) don't work together?

Questions by pbchaudhari   answers by pbchaudhari

Showing Answers 1 - 2 of 2 Answers

baseersd

  • Sep 18th, 2007
 

This is a general problem of the keyboard buffer. When scanf("%d",&n); is used and when an input is entered, the newline character("n") along with the input is stored in buffer. This buffer gets clear as the user presses "Enter" or if the buffer is full.

So when we use the scanf("%d",&n); the "n" is left in the buffer itself. So the buffer will fill the string with "". Hence it will not print anything in place of string.

The Following code will help in understanding this point.

#include<stdio.h>
int main()
{
 int n;
 char string[20];

printf("String ::%s",string);

 printf("Enter the number");
 scanf("%d",&n);

 printf("nEnter the string");
 gets(string);
 
 printf("Number ::%d",n);
 printf("String ::%s",string);
if(strcmp(string,"") == 0)
printf("Null string");
 system("pause");
 return 0;   
}

Here initially i am trying to print the contents of the string.... It gives garbage.Later
in the output Null string will be displayed. Which means the string is containing "".

Note :
When we change the order, ie. gets(string); and then scanf(%d",&n); we will not have this problem.

Solution :
The solution for such problems is to use fflush(stdin); This clears the keyboard buffer. Hence we can get the desired output. This fflush() should be used after entering the input. ie after scanf() and before gets().

#include<stdio.h>
int main()
{
 int n;
 char string[20];
 
 printf("String ::%s",string);
 printf("Enter the number");
 scanf("%d",&n);
 fflush(stdin);
 printf("nEnter the string");
 gets(string);
 
 printf("Number ::%d",n);
 printf("String ::%s",string);
if(strcmp(string,"") == 0)
printf("Null string");
 system("pause");
 return 0;   
}

Hope i answered correctly. If you have any doubt feel free to ask it.



  Was this answer useful?  Yes

kbjarnason

  • Jul 1st, 2010
 

The solution is *NOT*, as another writer posted, to use fflush(stdin), because fflush is defined only for *output* streams, and stdin is not an output stream.  Yes, your implementation may support fflush on input streams, but the C language does not.

On another note, there is no - I repeat NO - excuse, ever, for ever using the function gets.  It is dangerous, it is evil, it is to be avoided at every possible opportunity.  That said, scanf is almost as bad.

What you almost certainly want to use is fgets to read the input into a buffer, then, say, strtol to parse numeric values, or sscanf to parse string values.  fgets has the advantages that it can't overflow you buffer (if you pass it the same size _as_ your buffer), and it indicates whether you got the whole input or not - if the last character in the buffer (other than the terminating ) is a 'n', you read the whole input; if the n isn't there, there's more data to come.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.