9

I recently stumbled upon a curious case(atleast for me, since I hadn't encountered this before)..Consider the simple code below:-

int x;
scanf("%d",&x);
printf("%d",x);

The above code takes a normal integer input and displays the result as expected..

Now, if I modify the above code to the following:-

int x;
scanf("%d ",&x);//notice the extra space after %d
printf("%d",x);

This takes in another additional input before it gives the result of the printf statement.. I don't understand why a space results in change of behaviour of the scanf().. Can anyone explain this to me....

Justicle
  • 14,761
  • 17
  • 70
  • 94
sachin11
  • 1,087
  • 3
  • 13
  • 17
  • 1
    scanf is EVIL and should be avoided - so say I, and others :) : http://stackoverflow.com/questions/456303/how-to-validate-input-using-scanf – Bukes Jun 14 '11 at 21:13
  • @Bukes: yes, if you don't know how to program, or read documentation, you should not use C – Chris Dodd Jun 14 '11 at 21:20
  • 1
    @Chris Dodd - `scanf` can't distinguish between newlines and other whitespace. If you want fine control over your input, or the ability to sanely recover from errors, you can't use `scanf` regardless of how well you know C. – Chris Lutz Jun 14 '11 at 21:23

3 Answers3

8

From http://beej.us/guide/bgc/output/html/multipage/scanf.html:

The scanf() family of functions reads data from the console or from a FILE stream, parses it, and stores the results away in variables you provide in the argument list.

The format string is very similar to that in printf() in that you can tell it to read a "%d", for instance for an int. But it also has additional capabilities, most notably that it can eat up other characters in the input that you specify in the format string.

What's happening is scanf is pattern matching the format string (kind of like a regular expression). scanf keeps consumes text from standard input (e.g. the console) until the entire pattern is matched.

In your second example, scanf reads in a number and stores it in x. But it has not yet reached the end of the format string -- there is still a space character left. So scanf reads additional whitespace character(s) from standard input in order to (try to) match it.

antinome
  • 3,408
  • 28
  • 26
3

From the man page:

The format string consists of a sequence of directives which describe how to process the sequence of input characters. If processing of a directive fails, no further input is read, and scanf() returns. A "failure" can be either of the following: input failure, meaning that input characters were unavailable, or matching failure, meaning that the input was inappropriate (see below).

   A directive is one of the following:

   ?      A  sequence of white-space characters (space, tab, newline, etc;
      see isspace(3)).  This directive matches  any  amount  of  white
      space, including none, in the input.
ribram
  • 2,392
  • 1
  • 18
  • 20
  • Note that this means that when there's a space in the format, scanf will read input. consuming and discarding whitespace characters until it finds a non-whitespace character. It will leave that character as the next to be read and then return. – Chris Dodd Jun 14 '11 at 21:28
0

man scanf

[...] A sequence of white-space characters (space, tab, newline, etc.; see isspace(3)). This directive matches any amount of white space, including none, in the input.

Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130
  • 1
    @Christian Rau: It is about pattern matching. A space `matches any amount of white space, [...], in the input`. Seems like an answer. – Sebastian Mach Jun 15 '11 at 08:11