3

When I don't include white space between %d and %c specification in the format string of scanf() function in the following program, and give input during run-time as "4 h", then the output is "Integer = 4 and Character= .

How exactly variable "c" takes the input in this case and what difference does it make if i include a white space between %d and %c specification ?

Code

#include <stdio.h>

int main()
{
    char c;
    int i;
    printf("Enter an Integer and a character:\n");
    scanf("%d %c",&i,&c);
    printf("Integer = %d and Character = %c\n",i,c);
    getch();
} 
EsmaeelE
  • 2,331
  • 6
  • 22
  • 31
Shubham_Dubey
  • 51
  • 1
  • 7
  • @petercordes I opened this, since I think this is another case, that I met again, namely putting `%c` without preceding space on a single line, there is no question about "newline in the buffer" so it is somewhat different. – Antti Haapala -- Слава Україні Jul 05 '20 at 06:32
  • @AnttiHaapala: I think I closed it as a duplicate of [scanf() leaves the new line char in the buffer](https://stackoverflow.com/q/5240789) because the ultimate problem is the same: `%c` itself (unlike other conversions) will read a space if that's the next character. But a literal space in the format string consumes an arbitrary amount of whitespace. Reopening is fine, though; the answer here is good. – Peter Cordes Jul 05 '20 at 07:05

2 Answers2

8

If you read the specification for scanf() carefully, most format specifiers skip leading white space. In Standard C, there are three that do not:

  • %n — how many characters have been processed up to this point
  • %[…] — scan sets
  • %c — read a character.

(POSIX adds a fourth, %C, which is equivalent to %lc.)

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

Adding the space between %d and %c means that optional white space is skipped after the integer is read and before the (not white space) character is read.

Note that literal characters in a format string (other than white space — for example, the X and Y in "X%dY") do not skip white space. Matching such characters does not count as a successful conversion either; they do not affect the return value from scanf() et al.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
1

A space before %c specifier in scanf instruct it to skip any number of white-spaces. In other words, read from standard input until and unless a non-white-space character or keyboard interrupt is found.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • 1
    This means that when i don't use the whitespace between %d and %c specifications then while taking the input as "4 h",compiler assings the "whitespace" to the char variable 'c' Am i correct ?? – Shubham_Dubey Apr 08 '16 at 16:07
  • @Shubhamdubey2111; Yes. Exactly. – haccks Apr 08 '16 at 16:08
  • Also, including/excluding the whitespace doesn't effect the program if two integers are in play instead of a character and an integer. Why ?? – Shubham_Dubey Apr 08 '16 at 16:39
  • That's work for `%c` specifier _only_? Certainly not consume leading white-space it applies to at least 2 other format specifiers. – chux - Reinstate Monica Apr 08 '16 at 17:20