Predict the Output: void main() { int arr[10] = { 1, 2, 3, 4, 5 };…
2026
Predict the Output:
void main()
{
int arr[10] = { 1, 2, 3, 4, 5 };
printf("%d", arr[5]);
}
Type '50' in the answer if there is compile time or run time error in the above code.
- A.
0
- B.
1
- C.
error
- D.
1 2 3 4 5
Attempted by 613 students.
Show answer & explanation
Correct answer: A
When an array is partially initialized at the time of declaration, the remaining elements are automatically initialized to 0 by default.
In the given code, arr[10] is declared and initialized with the first five elements: 1, 2, 3, 4, 5.
The elements from index 5 to 9 are not explicitly assigned values, so they are set to 0.
The printf statement prints arr[5], which is the sixth element of the array.
Since arr[5] is 0, the output of the program is 0.