vara
vara

Reputation: 836

Explain what is difference between without whitespace in scanf and with whitespace in scanf?

main()
{
    int d,a;
    printf("Enter the digit :");
    scanf("%d",&d);
    printf("Enter another digit :");
    scanf("%d",&a);
}

output: Enter the digit : 10 Enter the another digit:10

main()
{
    int d;
    char a[10];
    printf("Enter the digit :");
    scanf("%d ",&d);
    printf("Enter another digit :");
    scanf("%s ",a);
}

output:

Enter the digit : 10
waiting for stdin 

Can anyone explain the difference between scanf("%d",&a) and scanf("%d ",&a)? Why does adding a space in the scanf statement cause it to wait of stdin?

Upvotes: 3

Views: 742

Answers (2)

rbhawsar
rbhawsar

Reputation: 813

Having a space in scanf means that it will expect a space. Hence it waits for you to enter the space.

Upvotes: -1

timos
timos

Reputation: 2707

A whitespace in a scanf format string matches any whitespace character, not only space, even multiple times, so if you press enter, it is a part of the matched string. If you press Ctl+D it should work.

Upvotes: 4

Related Questions