What is printed by this C code: int a = {1, 2}; printf(“%d”, a);?
Correct answer B. 0
Explanation
When an array has an initialiser list shorter than the array, the remaining elements are initialised as if they had static storage duration, meaning they become zero. So a is {1, 2, 0, 0, 0} and a[3] is 0. Only an array declared with no initialiser at all would contain indeterminate values. This rule is why int a[100] = {0}; zeroes an entire array.
Answer checked How we write and check questions
Discussion