For int a[5] = {1, 2}; what value does a[4] hold in C?
Correct answer
D. 0, because the remaining elements are zeroed
14 views
D. 0, because the remaining elements are zeroed
14 views
When an initialiser list is shorter than the array, C zero-initialises every remaining element, so a[2], a[3] and a[4] all become 0. This applies to static and automatic arrays alike, unlike an uninitialised local array whose elements hold indeterminate values. The rule is why writing int a[100] = {0}; is a reliable way to clear a whole array.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is D? Ask here and get an explanation.