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.