What is the output of the following code snippet? int arr[] = {1, 2, 3, 4, 5};…

2024

What is the output of the following code snippet?

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

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

  1. A.

    1

  2. B.

    5

  3. C.

    Undefined behaviour

  4. D.

    0

Attempted by 926 students.

Show answer & explanation

Correct answer: C

The code tries to access arr[5], which is out of bounds for the array declared. Arrays in C do not perform boundary checking, so trying to access outside its limit leads to undefined behavior, which could be anything like garbage value or a segmentation fault.

Explore the full course: Coding For Placement