What will be the output of the following C code? int arr[] = {10, 20, 30, 40};…

2025

What will be the output of the following C code?

int arr[] = {10, 20, 30, 40};
for(int i = 0; i < 4; i++) {
printf("%d ", arr[i]);
}

  1. A.

    0 1 2 3

  2. B.

    10 20 30 40

  3. C.

    Compilation error

  4. D.

    arr[0] arr[1] arr[2] arr[3]

Attempted by 65 students.

Show answer & explanation

Correct answer: B

The code initializes an integer array named arr with four elements: 10, 20, 30, and 40. The for loop iterates from i = 0 to i < 4, accessing each element sequentially using the index notation arr[i]. During the first iteration (i=0), it prints arr[0], which is 10. In the second iteration (i=1), it prints arr[1], which is 20, and so on until the fourth iteration (i=3), printing arr[3], which is 40. The printf statement outputs each number followed by a space, resulting in the sequence "10203040". Option A is incorrect because it lists the loop indices rather than array values. Option C is wrong as there are no syntax errors in this standard C code. Option D is incorrect because the code prints integer values, not variable names or string literals.

Explore the full course: Rssb Basic Computer Instructor