在Linux操作系統Ubuntu 14.04裏面「如果循環」,我想計數輸入的線路:打印纔有一次「while循環」
#include <stdio.h>
/*count lines in input*/
main()
{
int c, nl;
nl = 0;
while ((c = getchar()) != EOF) {
if (c == '\n')
nl = nl + 1;
printf("input lines are %d\n", nl);
}
}
輸出卻是:
asdsadsa
input lines are 0
input lines are 0
input lines are 0
input lines are 0
input lines are 0
input lines are 0
input lines are 0
input lines are 0
input lines are 1
asdasd
input lines are 1
input lines are 1
input lines are 1
input lines are 1
input lines are 1
input lines are 1
input lines are 2
正確地設置代碼的格式。提交前請檢查預覽窗格,並在編輯時閱讀「如何格式化」方框 –
輸出看起來對輸入和代碼是正確的,您還應該解釋您期望看到的內容以及您實際看到的內容有何不同 –
'if'不是循環/迭代語句。並且使用非原型聲明器已被棄用。 – Olaf