2012-10-03 60 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

相關問題