Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between scanf("%c", &c) and scanf(" %c", &c) [duplicate]

Tags:

c

scanf

Consider the following C code snippet:

#include <stdio.h>

int main()
{
    int a;
    char c;
    scanf("%d",&a);
    scanf("%c",&c);
    printf("int=%d\n",a);
    printf("char=%c\n",c);
}

I'm able to input only the integer and not the character.The output is simply the integer value and no value is output for the second printf statement.

However if I use a space before the format specifier:

scanf(" %c",&c);

It works as expected. Why is this the case?

Someone told me it has something to do with clearing the input buffer. Could someone shed some light on the same?

like image 717
passmaster10 Avatar asked Aug 28 '13 14:08

passmaster10


People also ask

What does %C do in scanf?

When a character is entered by the user in the above program, the character itself is not stored. Instead, an integer value (ASCII value) is stored. And when we display that value using %c text format, the entered character is displayed.

What is the difference between %C and %S in C?

"%s" expects a pointer to a null-terminated string ( char* ). "%c" expects a character ( int ).

What does %c mean in C programming?

While it's an integer, the %c interprets its numeric value as a character value for display. For instance for the character a : If you used %d you'd get an integer, e.g., 97, the internal representation of the character a. vs. using %c to display the character ' a ' itself (if using ASCII)

Why is there a space before %C in scanf?

Space before %c removes any white space (blanks, tabs, or newlines). It means %c without space will read white sapce like new line(\n), spaces(' ') or tabs(\t). By adding space before %c, we are skipping this and reading only the char given.


2 Answers

The difference between scanf("%c", &c1) and scanf(" %c", &c2) is that the format without the blank reads the next character, even if it is white space, whereas the one with the blank skips white space (including newlines) and reads the next character that is not white space.

In a scanf() format, a blank, tab or newline means 'skip white space if there is any to skip'. It does not directly 'clear the input buffer', but it does eat any white space which looks similar to clearing the input buffer (but is quite distinct from that). If you're on Windows, using fflush(stdin) clears the input buffer (of white space and non-white space characters); on Unix and according to the C standard, fflush(stdin) is undefined behaviour.

Incidentally, if you typed the integer followed immediately by a carriage return, the output of your program ends with two newlines: the first was in c and the second in the format string. Thus, you might have seen:

$ ./your_program
123
int=123
char=

$

That is, the scanf() reads the newline as its input. Consider an alternative input:

$ ./your_program
123xyz
int=123
char=x
$

The integer input stopped when it read the 'x'; the character input therefore reads the 'x'.

like image 133
Jonathan Leffler Avatar answered Sep 28 '22 17:09

Jonathan Leffler


Because after you input the number and press ENTER, the new line stays in the buffer and will be processed by the second scanf.

In short, you saved new line in the variable c.

However ,if you use

scanf(" %c",&c);
//     ^

the space will consume the new line, which makes c the value you expected.

like image 25
Yu Hao Avatar answered Sep 28 '22 16:09

Yu Hao