What will be the output of the given program? void main() { int arr[10] = { 1,…

2025

What will be the output of the given program?

void main()

{

int arr[10] = { 1, 2, 3, 4, 5 };

printf("%d", arr[5]);

}

Type '100' as the answer if there is any compile time or run time error in the above code.

  1. A.

    error

  2. B.

    5

  3. C.

    1 2 3 4 5

  4. D.

    0

Attempted by 582 students.

Show answer & explanation

Correct answer: D

When an array is partially initialized, the remaining elements are automatically set to 0. In this case, arr[10] is initialized with values 1, 2, 3, 4, 5 at indices 0 to 4, so arr[5] to arr[9] are all 0.

The printf statement uses %d to print a single integer. Since arr[5] is the sixth element and not explicitly initialized, it holds the default value of 0. Therefore, the output will be 0.

Explore the full course: Coding For Placement