这将是未初始化变量的值? [英] What will be the value of uninitialized variable?
问题描述
可能重复:结果
是很好的说明未初始化的数据的行为?
我尝试以下code
#include<stdio.h>
void main()
{
int i; \
printf('%d',i);
}
结果在VC ++给了垃圾的价值,而在同一TC为零。
会有什么正确的价值?
在默认情况下未初始化的变量具有零价值?或者它将包含垃圾的价值?
The result gave garbage value in VC++, while same in tc was zero. What will be the correct value? Will an uninitialized variable by default have value of zero? or it will contain garbage value?
下一步是在相同的
#include<stdio.h>
void main()
{
int i,j,num;
j=(num>0?0:num*num);
printf("\n%d",j);
}
将高于code的输出是什么?
What will be the output of the code above?
推荐答案
从技术上讲,一个未初始化的非静态局部变量的值不确定 [参考1] 。< BR>
总之可以是任何东西。访问这样一个未初始化的变量会导致一个未定义行为 [参考文献2]
Technically, the value of an uninitialized non static local variable is Indeterminate[Ref 1].
In short it can be anything. Accessing such a uninitialized variable leads to an Undefined Behavior.[Ref 2]
[参考1] 结果
C99部分6.7.8初始化:
如果具有自动存储时间的对象没有明确初始化,它的价值是不确定的。
If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate.
[参考文献2]
C99部分3.18理解过程网络斯内德行为:
行为,在使用一不可移植的或错误的程序结构,错误的数据,或 indeterminately估价对象,对于这本国际标准并没有规定要求。
behavior, upon use of a nonportable or erroneous program construct, of erroneous data, or of indeterminately valued objects, for which this International Standard imposes no requirements.
注:重点煤矿
这篇关于这将是未初始化变量的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!