2012-10-03 57 views
3
main() 
{ 
    int d,a; 
    printf("Enter the digit :"); 
    scanf("%d",&d); 
    printf("Enter another digit :"); 
    scanf("%d",&a); 
} 

输出: 输入数字:10 输入另一个数字:10解释scanf中没有空格和scanf中空白之间有什么区别?

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

输出:

Enter the digit : 10 
waiting for stdin 

任何人都可以解释scanf("%d",&a)scanf("%d ",&a)之间的区别?为什么在scanf语句中添加空格会导致它等待stdin?

回答

4

scanf格式字符串中的空格匹配任意空格字符,不仅空格,甚至多次,所以如果按Enter键,则它是匹配字符串的一部分。如果你按Ctl + D它应该工作。

-1

scanf中有一个空格意味着它会占用一个空间。因此它等着你进入这个空间。

+1

该指令(空格)与输入中的任意数量的空白(包括无)匹配。所以不,你不必输入空格字符。 – timos

相关问题