#include <stdio.h>
int main(void) {
char arr[10];
gets(arr);//let's suppose input is "abcdefgshsdfhdsfsh"
printf("%s",arr);//output come is same as input but the allocated arr have 10 bytes memory
//and can store only ten characters but how come it's possible i am getting output
//more than 10 character
return 0;
}
输出背后的原因是什么?任何帮助,将不胜感激。任何人都可以解释为什么输出超过了内存分配?
字符串最后需要'\ 0'。你在这里展示的是未定义的行为。 – 101010 2014-10-02 06:09:46
@ 40two:'gets()'终止于用'\ 0''读取的数据。 – alk 2014-10-02 06:19:48
在没有解释**的情况下关闭与UB的这种引用为什么**这是UB没有意义。 – alk 2014-10-02 06:21:52