Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

initial value of int array in C

People also ask

What is the initial value of array in C?

For instance, the integer arrays are initialized by 0 . Double and float values will be initialized with 0.0 . For char arrays, the default value is '\0' . For an array of pointers, the default value is nullptr .

What are int arrays initialized in C?

The array will be initialized to 0 in case we provide empty initializer list or just specify 0 in the initializer list.

What is the initial value of int in C?

The default value of Integer is 0.


If the array is declared in a function, then the value is undefined. int x[10]; in a function means: take the ownership of 10-int-size area of memory without doing any initialization. If the array is declared as a global one or as static in a function, then all elements are initialized to zero if they aren't initialized already.


As set by the standard, all global and function static variables automatically initialised to 0. Automatic variables are not initialised.

int a[10];  // global - all elements are initialised to 0

void foo(void) {
    int b[10];    // automatic storage - contain junk
    static int c[10]; // static - initialised to 0
}

However it is a good practice to always manually initialise function variable, regardless of its storage class. To set all array elements to 0 you just need to assign first array item to 0 - omitted elements will set to 0 automatically:

int b[10] = {0};

Why are function locals (auto storage class) not initialized when everything else is?

C is close to the hardware; that's its greatest strength and its biggest danger. The reason auto storage class objects have random initial values is because they are allocated on the stack, and a design decision was made not to automatically clear these (partly because they would need to be cleared on every function call).

On the other hand, the non-auto objects only have to be cleared once. Plus, the OS has to clear allocated pages for security reasons anyway. So the design decision here was to specify zero initialization. Why isn't security an issue with the stack, too? Actually it is cleared, at first. The junk you see is from earlier instances of your own program's call frames and the library code they called.

The end result is fast, memory-efficient code. All the advantages of assembly with none of the pain. Before dmr invented C, "HLL"s like Basic and entire OS kernels were really, literally, implemented as giant assembler programs. (With certain exceptions at places like IBM.)


According to the C standard, 6.7.8 (note 10):

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate.

So it depends on the compiler. With MSVC, debug builds will initialize automatic variables with 0xcc, whereas non-debug builds will not initialize those variables at all.


A C variable declaration just tells the compiler to set aside and name an area of memory for you. For automatic variables, also known as stack variables, the values in that memory are not changed from what they were before. Global and static variables are set to zero when the program starts.

Some compilers in unoptimized debug mode set automatic variables to zero. However, it has become common in newer compilers to set the values to a known bad value so that the programmer does not unknowingly write code that depends on a zero being set.

In order to ask the compiler to set an array to zero for you, you can write it as:

int array[10] = {0};

Better yet is to set the array with the values it should have. That is more efficient and avoids writing into the array twice.