What is the effect of trailing white space in a scanf() format string?

后端 未结 4 1730
小蘑菇
小蘑菇 2020-11-21 07:01

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

相关标签:
4条回答
  • 2020-11-21 07:17

    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
    
    0 讨论(0)
  • 2020-11-21 07:30

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-21 07:34

    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.

    0 讨论(0)
提交回复
热议问题