1

Below is a program which accepts two character and prints them

#include<stdio.h>
int main()
{
    char c1, c2;
    printf("\n Enter characters one : ");
    scanf(" %c", &c1);
    printf("\n Enter character two : ");
    scanf("%c", &c2);
    printf("\n The the characters are %c and %c ", c1, c2);
    return 0;
}

Now one output instance is :-

Enter two characters : a
The the characters are a and

The problem is that I haven't given any space between two format specifier %c Here I pressed 'a' and then '\n' which gets stored into c1 and c2 respectively. And thus I got output which was not accepted.

I know how to correct this problem.

Now I make the same program for the integers :-

#include<stdio.h>
int main()
{
    int a, b;
    printf("\n Enter two numbers : ");
    scanf("%d%d", &a, &b);
    printf("\n The two numbers are %d and %d ", a, b);
    return 0;
}

Here we will not found any problem.

I think this time we didn't encounter problem because the second input we give is '\n' or space which is not any integer and thus we get a failure in the reading from the scanf() function so the input buffer is still active and if we press the next input as integer then it gets stored into variable 'b'.

Can you tell me the reason which I thought is correct or not?

Now if it is correct then what will happen if I press again a character. Then also it should not get stored into the variable 'b' but this time 0 gets stored into variable 'b'.

So my question is that what is the reason for proper behavior of the program when I'm trying to make the same program with %d

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
raja THE KING
  • 61
  • 1
  • 8
  • 4
    `scanf("%c", &c2);` --> `scanf(" %c", &c2);` with the space just like the first usage. The `%d` format skips whitespace, the `%c` format does not. The `space` makes it do so. – Weather Vane Dec 30 '15 at 20:04
  • Possible duplicate of [C: Multiple scanf's, when I enter in a value for one scanf it skips the second scanf](http://stackoverflow.com/questions/9562218/c-multiple-scanfs-when-i-enter-in-a-value-for-one-scanf-it-skips-the-second-s) – Thomas Padron-McCarthy Dec 30 '15 at 20:07
  • no it's not... I have gone through that and after that i asked this question – raja THE KING Dec 30 '15 at 20:10

2 Answers2

3

To answer your question, let's have a look at C11 standard, chapter §7.21.6.2

Input white-space characters (as specified by the isspace function) are skipped, unless the specification includes a [, c, or n specifier.

So, when you have a newline ('\n', which is indeed a white-space character) left in the input buffer,

  • in case of scanf("%c", &charVar);, the newline is considered as the input, so the second scanf skips asking for the input from user.

  • in the case of scanf("%d", &intVar); the leftover newline is skipped and it wait for the integer input to appear, so it stops and asks the user for the input.

However, FWIW, in later case, if you input a non-whitespace character and press ENTER, the char input will be considered as an input, causing a matching failure.

Related

[...] If the input item is not a matching sequence, the execution of the directive fails: this condition is a matching failure.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • from where do you get to see `C11 standard, chapter §7.21.6.2`? – raja THE KING Dec 30 '15 at 21:07
  • @rajaTHEKING Sorry I did not get you. What do you mean _from where_? It is in the standard document. I've N1570. Which one you're referring to? Based on the revision, chapter numbering maybe slightly different. – Sourav Ghosh Dec 30 '15 at 21:10
  • @raja THE KING 2 I googled "Input white-space characters (as specified by" and came up with many hits including [this](http://www.gnu.org/software/libc/manual/html_node/String-Input-Conversions.html) – chux - Reinstate Monica Dec 30 '15 at 21:10
  • @SouravGhosh I was just asking for the link of that `C11 standard`. So that I can use that to refer many other things. Anyways thanks a lot for your answer – raja THE KING Dec 30 '15 at 21:14
  • 1
    @rajaTHEKING I see. Actual standard document costs money because you've to buy it, however the draft versions are pretty good, too. You can find an [online version](http://port70.net/~nsz/c/c11/n1570.html) here. – Sourav Ghosh Dec 30 '15 at 21:18
0

It's reading the new line as the newline is a character. Simply changing scanf("%c", &c2); to scanf(" %c", &c2); will make it work.

So in your code: #include

int main()
{
    char c1, c2;
    printf("\n Enter characters one : ");
    scanf(" %c", &c1);
    printf("\n Enter character two : ");
    scanf(" %c", &c2);
    printf("\n The the characters are %c and %c ", c1, c2);
    return 0;
}
Dom
  • 1,687
  • 6
  • 27
  • 37
  • I already know how to make that work, but I want to know that what difference is being created when I'm using %d format specifier. And specially the reason for it. – raja THE KING Dec 30 '15 at 20:07
  • 2
    @rajaTHEKING The new line cannot be parsed as an int which makes the diffrence in this case. – Dom Dec 30 '15 at 20:08
  • okay, I think that the new line can't be parsed into int because its a character ('\n') .. right? – raja THE KING Dec 30 '15 at 20:15
  • And if this is the reason then all the next inputs which are character should also not be parsed as the int. – raja THE KING Dec 30 '15 at 20:21
  • @rajaTHEKING It looks for the number character (0-9) and views the newline character as a separator between the two formatting flags as it also would a space. If you decided you wanted to use the character 'q' to separate the two numbers you would get like in this string `1q2`, you would get 1 and 0 as an answer because it comes to a character that is not a number or a separator and thus is junk so it just gives you 0 back because what you tried to put in the formatting flag doesn't make sense.. – Dom Dec 30 '15 at 20:27