Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

int array[30] = {0}; How this works in c?

How this works(sets all values to 0)?

int array[28]= {0};

and why this not works(does not set all values to 4 but only the first value sets to 4 and others to 0)?

int array[28]= {4};
like image 827
Md Abdulla Al Mamun Nayon Avatar asked Nov 28 '22 14:11

Md Abdulla Al Mamun Nayon


1 Answers

The elements which are not initialized are set 0. In your first case you are initilizing it by providing it the value as 0 and rest are by default initialized as 0. In your second case the first values is intialized with 4 and rest as 0. The standard says:

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.

like image 64
Rahul Tripathi Avatar answered Dec 05 '22 10:12

Rahul Tripathi