Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

array initialization in C/C++

Tags:

c++

c

does this code initialize all the elements of the array in all major C and C++ compilers or not?

int arr[100] = {0};
like image 834
MBZ Avatar asked Dec 29 '25 23:12

MBZ


1 Answers

In all compilers. This is guaranteed by the C Standard and the C++ Standard.

For example, for C here is the relevant paragraph:

(C99, 6.7.8p21) "If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration."

and

(C99, 6.7.8p10) "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: [...] if it has arithmetic type, it is initialized to (positive or unsigned) zero; [...]"

like image 175
ouah Avatar answered Jan 01 '26 13:01

ouah