Scanf搞乱了我的字符数组?

Scanf搞乱了我的字符数组?,c,arrays,scanf,C,Arrays,Scanf,我有以下代码: #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { int i=0; char x[]="q"; char b[]="f"; printf("Enter a letter:"); scanf(" %s", x); while(i<5) { printf("%c\n", x[i]); i++; }

我有以下代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
  int i=0;
  char x[]="q";
  char b[]="f";

  printf("Enter a letter:");
  scanf(" %s", x);

  while(i<5)
  {
    printf("%c\n", x[i]);
    i++;
  }

  i=0;

  while(i<5)
  {
    printf("%c\n", b[i]);
    i++;
  }

  return 0;
}
#包括
#包括
#包括
int main()
{
int i=0;
字符x[]=“q”;
字符b[]=“f”;
printf(“输入字母:”);
scanf(“%s”,x);

而(i两个
x
b
数组都是只有两个元素的数组,并且(例如)
x
数组被初始化为
'q'
'\0'
。如果您输入的字符多于一个,以便
scanf
读取,您将写入超出数组
x
的范围,从而导致未定义的行为


打印时,您也会越界访问数组,再次导致未定义的行为。在索引
5
处开始索引的
b
数组请学习如何使用调试器,以便将来至少能够尝试帮助自己。