When I use scanf to get two input values like below, I find that when I enter two values, like 1 2, the program doesn't continue, it gets stuck.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a = 0, b = 0;
scanf("%d %d ", &a, &b);
printf("%d\n%d\n", a, b);
system("pause");
return 0;
}
The only way the code works is if I input 1 2 1.The last 1 can be any valid character other than a space, TAB, or carriage return.
I found the reason of the problem is an extra blankspace in the line of scanf. So if I change the line like this, it works fine. But I don't understand why. Please explain this.
scanf("%d %d", &a, &b);