我對C編程非常陌生。我的先生給了這個代碼找到最多n個數字。當我像Sir先生說的那樣,事情是完美的時候,例如在行中寫一個數字 - 輸入數字並在7,8 9 10行中輸入數字時,輸入數字彈出。瞭解scanf行爲
#include <stdio.h>
main()
{
int n, max, number, i;
printf("Type the number of numbers");
scanf("%d", &n);
if(n>0)
{
printf("Type the numbers");
scanf("%d",&number);
max=number;
for(i=1; i<n; i++)
{
scanf("%d", &number);
if(number>max)
max=number;
}
printf("MAX=%d \n", max);
}
}
但是如果我寫假設 - 5 8 9 10 7 6 - 然後程序理解它喜歡 - 它把N = 5,然後把數= 8然後循環執行號變爲9然後數字變成10日至6日,然後gves最大。
那麼如何scanf在這裏工作?儘管它們被連續寫入空格,它會單獨佔用數字嗎?
您可能還會閱讀'scanf'的一般頁面 - Google它編輯這裏是https://linux.die.net/man/3/scanf –
空格和換行符是相同的:它分隔數字。你也可以試試TAB。他們被稱爲「白色空間」。 – ZDF
'scanf'的'%d'按預期工作,因爲它跳過了以前的空白字符。 – BLUEPIXY