Why doesn't the value of the second variable matter after adding a space or a newline?

I am new to programming learning C language. I'm a little confused right now. I've tried googling about this but can't find a satisfying result, so I figured it out by asking a question on this site. Look at this short program

#include<stdio.h>
int main()
{
    int num1,num2;
    printf("enter the value of num1 and num2:");
    scanf("%d %d",&num1,&num2);
    printf("num1 = %d and num = %d",num1,num2);
    return 0;
}

      

When I enter a value For example - 215-15 without space

or new line

than it gives output num1 = 215 and num2 = -15 , but when I enter space

or new line

between 215 - and 15 , then it gives output num1 = 215 and num2 = -175436266 (or any unexpected number).

I know that when scanf () reads any character that is not in the conversion specification category, it will return that character and process other inputs. But in the first case it -(minus sign)

appears to be inappropriate input according to the conversion but it shows the correct output, but in the later case it does not show the correct output. Why?

+3


source to share


2 answers


Because it 215- 15

can only match one number: 215. As soon as it scanf()

reads -

, it stops processing the first match, since it -

cannot be a digit of the current number, so it num1

matches 215.

Then the number can no longer match, because you are left with - 15

. scanf()

reads a -

followed by a space, so there is no valid number to parse, and it returns (after pressing space and dash). It doesn't assign anything num2

, so what you see when you print is garbage.

So why does it work with 215-15

?



Space matters. C 215-15

, scanf()

again matches the first number 215, but now you are left with -15

in the input (not - 15

as in the previous example). -15

does not have a space between the sign and the first digit of the number, so it scanf()

sees it as a real number and parses it successfully.

In short, in both examples it is -

interpreted as the sign of the number for the next match. But it %d

does not ignore whitespace between the digits of a number, or between sign and digits (although it ignores any amount of spaces before starting the number, that is, either before the first digit or before the sign). So, if it sees -

followed by a space, it fails. If it sees -

followed by one or more digits, it successfully matches the number and consumes digits until a character that is not a digit is found.

+7


source


I think what is happening is documented in scanf at cplusplus.com.

Any character that is not a space character (empty, newline, or tab) or part of a format specifier (which starts with a% character) causes the function to read the next character from the stream, compare it to that character without spaces, and if it matches, it is discarded and the function continues with the next format character. If the character does not match, the function fails, returns and leaves subsequent characters in the stream unread.

Besides,



A single whitespace expression in the format string checks for any number of whitespace characters extracted from the stream (including none).

The scan format string is "% d% d". It expects a number, it throws out spaces and another number. After the first number, the "-" character was read does not match the format specifier, so scanf worked earlier, leaving the num2 variable uninitialized.

If you check the return value of scanf it will fail.

+1


source







All Articles