scanf() curious behaviour!

Viewed 1231

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....

3 Answers
Related