I'm new to C. If I write:
long my_array[100]; // global variable
Does C initialize the values to zero, or do I have to it manually initialize to zero?
I'm new to C. If I write:
long my_array[100]; // global variable
Does C initialize the values to zero, or do I have to it manually initialize to zero?
Global variables are automatically zero-filled (and do not need to be initialized if this is the intent). It is required by the standard (§6.7.8/10).
The standard for uninitialized static objects in section 6.7.8.10 of the C99 standard says:
"If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static storage duration is not initialized explicitly, then:
It depends on the context.
If the array is declared with static storage duration (in file scope or in block scope with explicit keyword static
), then it is guaranteed to be zero-initialized. Otherwise, it is not initialized at all.
You have not provided any context. However, the comment suggests that this is supposedly a "global variable", which implies that it is declared in file scope. If so, it is zero-initialized.