Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple C scanf does not work? [duplicate]

Tags:

c

console

scanf

If I try something such as:

int anint;
char achar;

printf("\nEnter any integer:");
scanf("%d", &anint);
printf("\nEnter any character:");
scanf("%c", &achar);
printf("\nHello\n");
printf("\nThe integer entered is %d\n", anint);
printf("\nThe char entered is %c\n", achar);

It allows entering an integer, then skips the second scanf completely, this is really strange, as when I swap the two (the char scanf first), it works fine. What on earth could be wrong?

like image 409
John Avatar asked Sep 19 '10 06:09

John


People also ask

Why does scanf not working in c?

You need to pass a valid pointer to it. Not a value. That's a common mistake most C learners make. So if you have a variable int x; and you want to scan something in it, use it like scanf(“%d”, &x); and not scanf(“%d”, x);

How do you use scanf with doubles?

To read a double, supply scanf with a format string containing the conversion specification %lf (that's a lower case L, not a one), and include a double variable preceded by an ampersand as the second parameter.

Can scanf take multiple inputs in c?

Inputting Multiple Values If you have multiple format specifiers within the string argument of scanf, you can input multiple values. All you need to do is to separate each format specifier with a DELIMITER - a string that separates variables.

Why scanf is not working in while loop?

That is because there is a left over newline character in the input buffer.


4 Answers

When reading input using scanf, the input is read after the return key is pressed but the newline generated by the return key is not consumed by scanf, which means the next time you read a char from standard input there will be a newline ready to be read.

One way to avoid is to use fgets to read the input as a string and then extract what you want using sscanf as:

char line[MAX];

printf("\nEnter any integer:");
if( fgets(line,MAX,stdin) && sscanf(line,"%d", &anint)!=1 ) 
   anint=0;

printf("\nEnter any character:");
if( fgets(line,MAX,stdin) && sscanf(line,"%c", &achar)!=1 ) 
   achar=0;

Another way to consume the newline would be to scanf("%c%*c",&anint);. The %*c will read the newline from the buffer and discard it.

You might want to read this:

C FAQ : Why does everyone say not to use scanf?

like image 156
codaddict Avatar answered Oct 09 '22 18:10

codaddict


The other answers are correct - %c does not skip whitespace. The easiest way to make it do so is to place whitespace before the %c:

scanf(" %c", &achar);

(Any whitespace in the format string will make scanf consume all consecutive whitespace).

like image 39
caf Avatar answered Oct 09 '22 18:10

caf


It doesn't skip the second scanf(); the second scanf() reads the newline left behind by the first scanf(). Most format codes skip white space; the %c format does not skip white space.

like image 5
Jonathan Leffler Avatar answered Oct 09 '22 20:10

Jonathan Leffler


calling getchar() before scanf will also purge the stored line break. More lightweight but more situational

char input_1;
char input_2;
getchar();
scanf("%c", &input_1);
getchar();
scanf("%c", &input_2);

will flush the line breaks, more useful in consecutive lines of code where you know it's only one queued value and not a string

like image 2
Dale Diaz Avatar answered Oct 09 '22 18:10

Dale Diaz