Bruce
Bruce

Reputation: 35285

Help with format string in scanf

Here's the statement:

int i;
scanf("%d ",&i);

Why does the space at the end of the format string causes scanf to accept two inputs instead of 1?

Upvotes: 2

Views: 188

Answers (1)

David Nehme
David Nehme

Reputation: 21597

The space at the end of the format string tells scanf to eat whitespace after the first integer. It's not actually accepting a second input. When you do enter a second value, scanf sees that the whitespace is finished it returns, storing the first integer into your variable i. The "second input" is still in the standard input stream.

Upvotes: 4

Related Questions