【发布时间】:2013-12-26 19:53:31
【问题描述】:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int a;
char c[1];
printf("\n%d\n", a);
gets(c);
printf("\n%d\n", a);
return 0;
}
当使用gets 读取c 时,之前打印的a 的值被打印为具有以下值:0;当使用scanf("%c", &c); 代替gets(c); 时,a 的值在整个代码中保持不变。
我似乎无法弄清楚为什么会这样,有人可以解释一下这是怎么可能的吗?
【问题讨论】:
-
gets本身就很奇怪。 -
'(使用 C,而不是 C++)' 那么为什么要标记 c++?重新标记...
-
打印未初始化的值是不好的。
-
when using scanf("%c",&c); as a replacement for gets(c); the value of a stays the same throughout the code.。a和c有什么关系? -
为什么有这么多反对票?当然,这是一个初学者的错误,但问题是具体的,措辞清晰,包含一个(诚然缩进严重的)示例案例,并且 OP 显然至少尝试修复它(他谈到了当他使用
scanf()时会发生什么)。向我 +1 以抵消不当的反对票。