我在 C 中有这段代码,它包含一堆char
s
#include<stdio.h>
# define NEWLINE '\n'
int main()
{
char c;
char str[6];
int i = 0;
while( ((c = getchar()) != NEWLINE))
{
str[i] = c;
++i;
printf("%d\n", i);
}
return 0;
}
输入是:testtesttest
输出:1 2 3 4 5 6 7 8 117 118 119 120
我的问题是:
尽管我明显超出了数组的容量,为什么我没有得到越界(分段错误)异常?
为什么输出中的数字突然跳到非常大的数字?
我在 C++ 中尝试过这个并得到了相同的行为。谁能解释一下这是什么原因?