以下是我的代碼,它不停止第一個索引,我想知道爲什麼?同樣是scanf
爲什麼scanf或fgetc無法正常工作
#include<stdio.h>
void main()
{
int n = 0, i = 0;
char arr[10];
printf("How many characters do u want to enter ");
scanf("%d", &n);
//printf("\n\t%d", n);
for(i = 0; i < n; i++)
{
printf("Enter %d character-->\n",i);
char ch = fgetc(stdin);
arr[i] = ch;
//putchar(arr[i]);
}
i = 0;
printf("You have entered characters are \n");
for(i = 0; i < n; i++)
{
printf("arr[%d] = %c\n", i, arr[i]);
}
printf("\n");
}
結果當我運行這段代碼我獲得以下
How many characters do u want to enter 5
Enter 0 character-->
Enter 1 character-->
程序不等待第一個字符輸入。
它應該是'int main(void)',而不是'void main()'。 –