Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

unix

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?

like image 980
vara Avatar asked Dec 07 '25 01:12

vara


1 Answers

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.

like image 172
timos Avatar answered Dec 08 '25 16:12

timos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!