简体   繁体   English

c中的char arrays的scanf还有另一个问题?

[英]yet another problem with scanf of char arrays in c?

I have this code that work:我有这个工作的代码:

 int main()
    {
        char a[100];
        int i;
        int n=3;
        for(i=1;i<=n;i++){
            printf("insert char \n");
            scanf("%c",&a[i]);
            getchar();
        }
        for(i=1;i<=n;i++){
            printf("%c ",a[i]);
        }
    }

I insert ab c he prints ab c, so far so good, I would like to be able to insert the size n with a scanf, but if I put a scanf of n like this:我插入 ab c 他打印 ab c,到目前为止一切顺利,我希望能够用 scanf 插入大小 n,但是如果我像这样放置 n 的 scanf:

int main()
        {
            char a[100];
            int i;
            int n;
            scanf("%d",&n);
            for(i=1;i<=n;i++){
                printf("insert char \n");
                scanf("%c",&a[i]);
                getchar();
            }
            for(i=1;i<=n;i++){
                printf("%c ",a[i]);
            }
        }

when I run the program, it causes problems in the second for loop, I enter ab c, and the second for returns empty characters,actually does newline n times,in C when each program finishes "PROCESS RETURNED etc."当我运行程序时,它在第二个for循环中导致问题,我输入ab c,第二个for返回空字符,实际上换行n次,在C中当每个程序完成“PROCESS RETURNED等”。 is printed.被打印。 When I run the first code the characters are printed on the same line and in the next line there is "PROCESS RETURNED", instead with the second code after the insertion n empty lines are printed and then "PROCESS RETURNED" even if I have not written \ n in the printf.当我运行第一个代码时,字符被打印在同一行,并且在下一行有“PROCESS RETURNED”,而不是插入后的第二个代码 n 空行被打印,然后“PROCESS RETURNED”即使我没有写在 printf 中。 I tried to see if n is stored in the same location as some element of a (with printf ("% p", & a [i]), or printf ("% p", & a) and printf ("% p", & n )), but the address is different.我试图查看 n 是否存储在与 a 的某些元素相同的位置(使用 printf ("% p", & a [i]) 或 printf ("% p", & a) 和 ZAFA0FF8B27B87666A6BDE8721 ("%pFDE71) ", & n )),但地址不同。 Some idea?有什么想法?

if first program since you scan chars first, there is nothing in buffer then you call getchar which will take \n as input, but in second program after entering int n you have one \n in buffer( scanf take only one integer and leave \n in buffer), so when you call scanf for your charecter it will take that \n as input.如果第一个程序因为您首先scan字符,缓冲区中没有任何内容,那么您调用getchar它将以\n作为输入,但是在输入int n后的第二个程序中,您在缓冲区中有一个\nscanf只取一个 integer 并离开\n在缓冲区中),因此当您为您的字符调用scanf时,它将将该\n作为输入。

so I suggest to add one space in scanf :所以我建议在scanf中添加一个空格:

 for(i=1;i<=n;i++){
                printf("insert char \n");
                scanf(" %c",&a[i]);//add space here
            }

You have to use getchar() after reading n because the newline character '\n' will remain in the input buffer and your second scanf in for loop will read that.您必须在读取n后使用getchar() ,因为换行符 '\n' 将保留在输入缓冲区中,而for循环中的第二个scanf将读取它。

Also you should start with index 0 .你也应该从索引0开始。 You can try the following code.你可以试试下面的代码。

int main()
 {
   char a[100];
   int i;
   int n;
   scanf("%d",&n);
   getchar();
  for(i=0;i<n;i++){
    printf("insert char \n");
    scanf("%c",&a[I]);
    getchar();
  }
  for(i=0;i<n;i++){
   printf("%c ",a[I]);
  }
 }

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

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