C语言 定义整数(int);默认值是多少?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4532871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Define integer (int); What's the default value?
提问by Naim Ibrahim
int i;
int data[5] = {0};
data[0] = i;
What's the value in data[0]?
data[0] 中的值是多少?
Also, what's the meaning of this line?
另外,这行是什么意思?
if (!data[0]) { ... }
回答by James McNellis
In most cases, there is no "default" value for an intobject.
在大多数情况下,int对象没有“默认”值。
If you declare int i;as a (non-static) local variable inside of a function, it has an indeterminate value. It is uninitialized and you can't use it until you write a valid value to it.
如果int i;在函数内部声明为(非静态)局部变量,则它具有不确定的值。它是未初始化的,在向其写入有效值之前不能使用它。
It's a good habit to get into to explicitly initialize any object when you declare it.
在声明任何对象时明确初始化它是一个好习惯。
回答by Jonathan Leffler
It depends on where the code is written. Consider:
这取决于代码在哪里编写。考虑:
int i;
int data[5] = {0};
void func1(void)
{
data[0] = i;
}
void func2(void)
{
int i;
int data[5] = {0};
data[0] = i;
...
}
The value assigned to data[0]in func1()is completely deterministic; it must be zero (assuming no other assignments have interfered with the values of the global variables iand data).
分配给data[0]in的值func1()是完全确定的;它必须为零(假设没有其他赋值干扰了全局变量i和的值data)。
By contrast, the value set in func2()is completely indeterminate; you cannot reliably state what value will be assigned to data[0]because no value has been reliably assigned to iin the function. It will likely be a value that was on the stack from some previous function call, but that depends on both the compiler and the program and is not even 'implementation defined'; it is pure undefined behaviour.
相比之下,设置的值func2()是完全不确定的;您无法可靠地说明将分配给什么值,data[0]因为i在函数中没有可靠地分配值。它可能是之前某个函数调用堆栈上的值,但这取决于编译器和程序,甚至不是“实现定义”;这是纯粹的未定义行为。
You also ask "What is the meaning of this?"
你还会问“这是什么意思?”
if (!data[0]) { ... }
if (!data[0]) { ... }
The '!' operator does a logical inversion of the value it is applied to: it maps zero to one, and maps any non-zero value to zero. The overall condition evaluates to true if the expression evaluates to a non-zero value. So, if data[0]is 0, !data[0]maps to 1 and the code in the block is executed; if data[0]is not 0, !data[0]maps to 0 and the code in the block is not executed.
' !' 运算符对其应用的值进行逻辑反转:它将零映射到 1,并将任何非零值映射到零。如果表达式的计算结果为非零值,则整体条件的计算结果为真。所以,如果data[0]为0,则!data[0]映射为1,执行块中的代码;如果data[0]不是 0,则!data[0]映射到 0 并且不执行块中的代码。
It is a commonly used idiom because it is more succinct than the alternative:
这是一个常用的习语,因为它比替代词更简洁:
if (data[0] == 0) { ... }
回答by JRK
If an integer is not initialized, its value is undefined as per C
如果一个整数没有被初始化,它的值按照 C 是未定义的
回答by Jerry Coffin
Since you've included the ={0};, the entire array is filled with zeros. If this is defined outside any function, it would be initialized with zeros even without the initializer. if (!data[x])is equivalent to if (data[x] == 0).
由于您包含了={0};,整个数组都填充了零。如果这是在任何函数之外定义的,即使没有初始化程序,它也会用零初始化。if (!data[x])相当于if (data[x] == 0)。
回答by mukesh
if an integer is declared globally then it is initialized automatically with zero but if it is local then contains garbage value until and unless itis given some value
如果一个整数是全局声明的,那么它会自动用零初始化,但如果它是本地的,则包含垃圾值,直到并且除非给出某个值
回答by Kevin Meredith
// File 'a.c'
//文件'ac'
#include <stdio.h>
void main()
{
int i, j , k;
printf("i = %i j = %i k = %i\n", i, j, k);
}
// test results
// 检测结果
> $ gcc a.c
> $ ./a.out
> i = 32767 j = 0 k = 0

