Public Group

# Problem with gets() and scanf in C.

This topic is 2116 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello,

I am having a problem when I use scanf and then gets().

Why do these two functions confilct together when used one after the other? Maybe I do something wrong?

for example:

char A[20],B[20];
int number;

printf("Number: ");
scanf("%d",&number);

printf("A: ");
gets(A);
printf("B: ");
gets(B);

the compiler just ignores gets(A)...
How can I solve this problem?

##### Share on other sites

scanf("%d") tells the program to get the standard input and load what's there as an integer and then leave the rest in the buffer. That means if your input is something like "9\n" then it just grabs the 9 but leaves the \n in the buffer. So when gets() comes along it sees the \n in the buffer and then decides it's done because gets() reads standard input until it sees a \n. One way to handle this is to use gets() to read the line that contains the number into a character array and then use sscanf() to parse that character array. Usual caveats about buffers sizes and error handling with C I/O apply.

##### Share on other sites
Note that scanf family is very nearly impossible to use safely, and should never be used in production code.  EVER.

Consider the input:  999999999999999

Then consider this line from the language standard:

If this object does not have an appropriate type, or if the result of the conversion cannot be represented in the object, the behavior is unde?ned.

Even if you limited it by something like %6d it is still possible to abuse the library in ways that fail horribly. Also if you ever face non-numeric input like "abcd" or "12a3" you will leave content in the stream. Then you must do extra work to remove the data from the stream and won't have any way to detect garbage data.

If you must read numbers that way, read a full string at once using fgets() and check for errors. If that succeeded use strtol() to convert it to a number and again check for errors such as the resulting number being only part of the string, out-of-range values, or not-numeric data. Then you probably want to double check that the user didn't accidentally use an octal notation (starting with zero) or maybe you want to keep that behavior.

##### Share on other sites

Yeah, scanf is bad. sscanf or fscanf though is good if you (yourself) write the string in the format you like before parsing it, it's one of my favourite functions. It makes parsing easy.

##### Share on other sites

Ok guys thanks for explaining me where the problem is. :P

##### Share on other sites

^What the moderators said above^

AND

One alternative solution to fix this problem is to use fflush to flush out the buffer.  This is what my C/C++ professor made us use to learn C.  However, this is non-standard; use it at your own discretion.

char A[20],B[20];
int number;

printf("Number: ");
scanf("%d",&number);

fflush (stdin); // flush out buffer

printf("A: ");
gets(A);
printf("B: ");
gets(B);


• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 11
• 23
• 42
• 75