代码
int main()
{
int n,m,i,j;char a[10][10];
printf("enter n and m values\n");
scanf("%d%d",&n,&m);
printf("enter array values");
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%c",&a[i][j]);
printf("the array is \n");
for(i=0;i<n;i++)
for(j=0;j<m;j++)
printf("%d %d %c\t",i,j,a[i][j]);
}
输入
Enter n and m values
4 5
Enter characters
11111000001111100000
输出
0 0
0 1 1 0 2 1 0 3 1 0 4 1 1 0 1 1 1 0 1 2 0 1 3 0 1 4 0 2 0 0
2 1 1 2 2 1 2 3 1 2 4 1 3 0 1 3 1 0 3 2 0 3 3 0 3 4 0
错误
如果我将n的值设为4,m的值设为5,scanf就会正常运行。
但是在打印时,当i的值为0且j的值为0时,它不会打印任何内容。
与此同时,a[0][1]打印第一个输入,a[0][2]打印第二个输入,依次类推,所以最后一个输入0在打印时被跳过了。
请解释一下为什么要避免a[0][0]。
fflush(stdout)
。 - sherrellbcfflush(stdout)
会对stdin
产生影响。 - chux - Reinstate Monicastdin
。 - sherrellbcscanf
函数的返回值。如果它小于您期望的值,并且您没有处理它,那么结果可能不会很好。 - hyde