int i;
int data[5] = {0};
data[0] = i;
What's the value in data[0]?
Also, what's the meaning of this line?
if (!data[0]) { ... }
int i;
int data[5] = {0};
data[0] = i;
What's the value in data[0]?
Also, what's the meaning of this line?
if (!data[0]) { ... }
In most cases, there is no "default" value for an int
object.
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.
It's a good habit to get into to explicitly initialize any object when you declare it.
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 i
and 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 i
in 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.
You also ask "What is the meaning of this?"
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.
It is a commonly used idiom because it is more succinct than the alternative:
if (data[0] == 0) { ... }
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
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)
.
// File 'a.c'
#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
As @James McNellis stated in 2010, the standards for programming languages leave the initial value of a variable to the compiler implementation, and results differ from one compiler implementation to another.
I came here to review the state of affairs in this regard because I remembered observing that some compilers initialize arrays of integers to zero.
After reading the inconclusive answers recorded here, I constructed a small static method in the assembly that I use to conduct such experiments before I implement a technique in production code. My current project requires porting some JavaScript code to C#. Since the JavaScript interpreter doesn't initialize anything, that implementation opened with a FOR loop to initialize a large array of integers. Before I blindly left it in the C# implementation.
The method follows.
private static void IntegerArrayGym ( )
{
int [ ] ints = new int [ 10 ];
Console.WriteLine ( $"{TEST_INTEGER_ARRAY_GYM} Begin:{Environment.NewLine}" );
int intArrayLength = ints.Length;
Console.WriteLine ( $" The test array consists of {ints} uninitialized integers.{Environment.NewLine}" );
for ( int intJ = ArrayInfo.ARRAY_FIRST_ELEMENT;
intJ < intArrayLength;
intJ++ )
{
Console.WriteLine ( $" Value of integer at ordinal position {ArrayInfo.OrdinalFromIndex ( intJ )} = {ints [ intJ ]}" );
} // for ( int intJ = ArrayInfo.ARRAY_FIRST_ELEMENT; intJ < intArrayLength; intJ++ )
Console.WriteLine ( $"{Environment.NewLine}{TEST_INTEGER_ARRAY_GYM} Done!" );
} // private static void IntegerArrayGym
The output follows.
CSharp_Lab, version 7.92.170.0 Begin @ 03/06/2023 22:58:49.124 (03/07/2023 04:58:49.124 UTC)
IntegerArrayGym Begin:
The test array consists of System.Int32[] uninitialized integers.
Value of integer at ordinal position 1 = 0
Value of integer at ordinal position 2 = 0
Value of integer at ordinal position 3 = 0
Value of integer at ordinal position 4 = 0
Value of integer at ordinal position 5 = 0
Value of integer at ordinal position 6 = 0
Value of integer at ordinal position 7 = 0
Value of integer at ordinal position 8 = 0
Value of integer at ordinal position 9 = 0
Value of integer at ordinal position 10 = 0
IntegerArrayGym Done! Please press the ENTER (Return) key to exit the program.