简体   繁体   English

C程序数组需要的输入过多

[英]C program array takes more input than it should

I was just playing around and i created a two D array in C of 3 rows and three columns and set a nested for loops to scan the elements. 我只是在玩耍,我在C中创建了一个3行3列的两个D数组,并设置了一个嵌套的for循环来扫描元素。 Now, a three by three matrix has Nine elements but this code is taking 10 inputs(even the for loop is running only 9 times) How is it happening??? 现在,一个三乘三的矩阵具有九个元素,但是此代码需要10个输入(即使for循环仅运行9次),这是怎么回事?

#include <stdio.h>


int main(){
int array[3][3];
int i,j;
for(i = 0; i < 3; ++i){
    for(j = 0; j < 3; ++j){
        printf("i = %d j = %d\n",i,j);
        scanf(" %d ",&array[i][j]);
        printf("i = %d j = %d\n",i,j);
    }
}

屏幕截图

scanf(" %d " --> scanf("%d"

Probably , the space after %d results in scanf waiting for another input. 可能是%d之后的空格导致scanf等待其他输入。 So, remove the space and it should work fine. 因此,删除空间,它应该可以正常工作。

scanf("%d ",&array[i][j]) ----> scanf("%d",&array[i][j])

这可能有助于解决它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM