我有scanf的问题,并给输入一个带for循环的char数组?

问题描述 投票:0回答:1

[当我运行该命令时,一切看起来都很好,在for循环中两次打印“写入字符”,然后等待输入。据我了解,这些值并没有真正分配(即使在第一次迭代中也没有)。有帮助吗?

c arrays for-loop char scanf
1个回答
0
投票

您的scanf函数正在捕获宽松的字符,也许是从printf字符串之前的,使用getchar()可以解决此问题,如下所示:

#include <stdio.h> 

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

fflush()应该有效,但是在您的代码中无效。

© www.soinside.com 2019 - 2024. All rights reserved.