38

What is difference between scanf("%d") and scanf("%d ") in this code, where the difference is the trailing blank in the format string?

#include <stdio.h>

int main(void)
{
    int i, j;

    printf("enter a value for j ");
    scanf("%d  ",&j);
    printf("j is %d\n", j);
    printf("enter a value for i ");
    scanf("%d", &i);
    printf("i is %d\n", i);
    return 0;
}

How does the scanf() function actually work when I add spaces after the format specifier like scanf("%d ", &j);?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Vikas Verma
  • 3,626
  • 6
  • 27
  • 40

4 Answers4

38

A whitespace character in a scanf format causes it to explicitly read and ignore as many whitespace characters as it can. So with scanf("%d ", ..., after reading a number, it will continue to read characters, discarding all whitespace until it sees a non-whitespace character on the input. That non-whitespace character will be left as the next character to be read by an input function.

With your code:

printf("enter a value for j ");

scanf("%d  ",&j);

printf("j is %d \n", j);

it will print the first line and then wait for you to enter a number, and then continue to wait for something after the number. So if you just type 5Enter, it will appear to hang — you need to type in another line with some non-whitespace character on it to continue. If you then type 6Enter, that will become the value for i, so your screen will look something like:

enter a value for j 5
6
j is 5
enter a value for i i is 6

Also, since most scanf %-conversions also skip leading whitespace (all except for %c, %[ and %n), spaces before %-conversions are irrelevant ("%d" and " %d" will act identically). So for the most part, you should avoid spaces in scanf conversions unless you know you specifically need them for their peculiar effect.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
6

A white-space character (space, newline, horizontal and vertical tab) in a format string matches any number of white-space characters in the input.
In your first case

  scanf("%d  ",&j);

when it encounters the white-space char (WSC) ' ' then it will eat all the white spaces input by user including \n on pressing Enter and it will expect to enter a non-WSC . In this case your program will terminate by pressing Ctrl + Z.

Wolf
  • 9,679
  • 7
  • 62
  • 108
haccks
  • 104,019
  • 25
  • 176
  • 264
4

A whitespace character in your scanf format matches any number of whitespace characters as described by isspace. So if you have tailing spaces, newlines, tabulators or any other whitespace character then it will also be consumed by scanf before it returns.

Sergey L.
  • 21,822
  • 5
  • 49
  • 75
  • 1
    This doesn't make the key point clear — that `scanf()` won't return until something that isn't white space is entered. That, in turn, means you have to guess what the program is going to ask next and enter the next value before the current input completes. (If you don't enter the complete next value, then you'll have more problems; the odd one character (and the newline that must follow before the terminal sends the character to the program) will be interpreted as the next field. Trailing spaces in interactive formats are dire! – Jonathan Leffler Sep 15 '15 at 15:04
0

The difference (although obvious) is a different format string. If you enter the following line:

"3  "

scanf() will return successfully. Otherwise, it depends on your input provided. scanf() essentially skips over whitespace (tabs, spaces, newlines), and searches for alphanumeric values in the input stream. Since this is trailing whitespace, it gets lumped in with the trailing newline character at the end of input when pressing ENTER, so it's of little consequence.

scanf() expects the input provided to exactly match the format string you provide to it, with the exception that contiguous whitespace characters are compressed to a single whitespace character. This becomes very important if you want to parse large strings of data with it's string-processing equivalent, sscanf().

A good exercise to further test this would be something like:

#include<stdio.h>

int main(void)
{
   int a=0,b=0,c=0;

   printf("Enter values for A, B, C, in the format: \"A B  -  C\"\n");
   scanf("%d %d  -  %d", &a, &b, &c);

   printf("Values: A:%d, B:%d, C:%d\n", a, b, c);
}

Afterwards, check and see what the values of these integers are after providing both correctly and incorrectly formatted consoled input (ie: spaces and hyphens). Here are a couple example runs. The first used incorrect input, the second used correctly formatted input. Notice that in the first case, C doesn't even get set, as scanf() will provided unexpected behavior if the input and the format strings don't match up. In general, you are better off using something like fgets() to get a string of input from the user, and then use various search functions (ie: strstr(), strch(), strcat, strcpy, etc) to parse your string, as it is much safer than just using scanf() and assuming the user won't make a mistake, either accidentally or deliberately.

Enter values for A, B, C, in the format: "A B  -  C"
1 2 3
Values: A:1, B:2, C:0

Enter values for A, B, C, in the format: "A B  -  C"
1 2  -  3
Values: A:1, B:2, C:3

Now, consider one last run: You'll see that scanf() compacts multiple consecutive whitespace characters to a single character, hence why these final runs actually succeeds:

Enter values for A, B, C, in the format: "A B  -  C"
1 2 - 3
Values: A:1, B:2, C:3

Enter values for A, B, C, in the format: "A B  -  C"
1     2           -                     3
Values: A:1, B:2, C:3
Cloud
  • 18,753
  • 15
  • 79
  • 153
  • 1
    -1: All the spaces in format strings in these examples are completely irrelevant and could be deleted with no change to the results, so this doesn't really answer the question, which is about a trailing space in the format string... – Chris Dodd Oct 21 '13 at 20:26
  • 1
    It does summarize how distinct whitespace characters are all lumped into a single one, and how the trailing whitespace in this particular example is of little consequence. It also describes how the format string essentially reduces to a pattern of expected whitespace and non-whitespace, and how assignment of values can occur to some or all target variables depending on when the input deviates from the format pattern. I don't think this was warranted. – Cloud Oct 23 '13 at 23:07
  • 1
    The code should record and report the return value from `scanf()`. That would show how many values have been set. The trailing white space in a format string is not "of little consquence" with interactive input. It means the user must type a character other than white space (and newline is white space) before the input is complete. – Jonathan Leffler Sep 15 '15 at 15:01
  • @ChrisDodd: one of the spaces before the dash in the format is significant; the others are not. If there is no space before the dash (e.g. `"%d%d-%d"`), then an input like `23 34 - 45` fails because the space in the input does not match the dash in the format (but `"%d%d -%d"` would be fine). Your main point is correct; most spaces are immaterial in a `scanf()` format string, but not all spaces are immaterial — and it is the subtlety of which are and which are not that bedevils the use of `scanf()` et al. And trailing blanks in format strings are especially pernicious! – Jonathan Leffler Sep 15 '15 at 15:13