0
int main()
{
   char a,b;
   scanf("%c",&a);
   getchar();
   scanf ("%c",&b);
}

If you don't use getchar for character scanf takes the whitespace as an input but for integer you don't need getchar:

int main()
{
   int a,b;
   scanf("%d",&a);
   scanf ("%d",&b);
}
kalpaj agrawalla
  • 878
  • 1
  • 9
  • 19
  • 2
    Is there a question here? – Chris Dodd Jan 18 '19 at 19:34
  • On what basis `scanf` will decide whitespace is not intended character entered by user? For number it knows that whitespace is not a number and it skips. – kiran Biradar Jan 18 '19 at 19:34
  • `scanf` is a very smart function. It skips the whitespaces when you try to read a number. – Roy Avidan Jan 18 '19 at 19:34
  • You don't need a `getchar` in the first case and it will break things. If you type "ab" into that code, it will read the "a", ignore the "b" and then read the enter as the second character and store it in `b`! That can't possibly be right. – David Schwartz Jan 18 '19 at 19:35

2 Answers2

1

Why don't you need a getchar() while getting integer as an input?

Interpreting your question in terms of the differences between the two provided examples, you seem to be asking about the difference between scanf's processing of %c directives and its processing of %d directives. At its simplest, the explanation is that scanf's specifications say that when it attempts to match a %d directive, it must skip any leading whitespace. Which, by the way, may comprise any number of characters, and which recognizes more characters than just ASCII 0x20 as whitespace.

The %c is actually the oddball here. Of all the scanf directives that match and convert input, it is one of only two that don't skip leading whitespace. This makes sense, because it allows scanf() to read space characters as input, and because you can instruct it to match (and therefore skip) leading whitespace by inserting a space character into the format string immediately before a %c (or any other) directive. Or you can read and ignore any single character, as your getchar() actually does, by inserting an additional %*c directive into the format.

The whitespace skipping performed for most other directives is a convenience catering to fixed-format tabular data, which may have varying amounts of space between individual items.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • tl;dr: %c does not skip whitespace while other conversion specifiers do. – Swordfish Jan 18 '19 at 20:01
  • `%[...]` likewise does not consume leading whitespace. – David C. Rankin Jan 19 '19 at 00:07
  • Yes, @DavidC.Rankin, that's the other of the two that don't. This answer alludes to it, but did not name it explicitly. – John Bollinger Jan 19 '19 at 00:11
  • Is enter also a white-space character? –  Jan 22 '19 at 14:04
  • 1
    @CyborgGamer, enter is a key on the keyboard, not a character. But typing it ordinarily causes a newline character (`'\n'`) to be sent, and yes, that is a whitespace character. – John Bollinger Jan 22 '19 at 16:36
  • What are whitespaces. Is '\0' also a whitespace? –  Jan 22 '19 at 19:18
  • 1
    @CyborgGamer, for the purposes of `scanf()`, whitespace characters are those for which the [`isspace()`](http://man7.org/linux/man-pages/man3/isalpha.3.html) function returns a nonzero value. These are locale-dependent, but in the default locale they are space, carriage return (`'\r'`), newline (`'\n'`), form feed (`'\f'`), horizontal tab (`'\t'`), and vertical tab (`'\v'`). The null character, `'\0'`, is not counted as whitespace in any locale I know, but if your data may contain null characters then *formatted* I/O functions such as `scanf` may not be the best choice. – John Bollinger Jan 22 '19 at 19:41
0

Why don't you need a getchar() while getting integer as an input?

"%d" skips leading white-space - including the left-over enter of the prior scanf("%c",&a);.
"%c" does not.

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

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256