Question:medium

Consider the following ANSI C program. 

#include <stdio.h>
int main(){
    int arr[4][5];
    int i, j;
    for (i=0; i<4; i++){
        for (j=0; j<5; j++){
            arr[i][j] = 10*i + j;
        }
     }
    printf("%d", *(arr[1] + 9));
    return 0;
} 

What is the output of the above program? 

 

Show Hint

In 2D arrays, pointer arithmetic continues linearly in row-major order, so adding beyond a row moves into the next row.
Updated On: Feb 2, 2026
  • 14
  • 20
  • 24
  • 30
Show Solution

The Correct Option is C

Solution and Explanation

Let's analyze the given ANSI C program step-by-step to determine the output. 

#include <stdio.h>
int main(){
    int arr[4][5];
    int i, j;
    for (i=0; i<4; i++){
        for (j=0; j<5; j++){
            arr[i][j] = 10*i + j;
        }
     }
    printf("%d", *(arr[1] + 9));
    return 0;
}

This code initializes a 2D array arr with 4 rows and 5 columns. The array is filled using nested loops. Let's break this down:

  1. The outer loop iterates through rows, and the inner loop iterates through columns. The expression arr[i][j] = 10*i + j; fills the array.
  2. During the first iteration (i=0):
    • j=0, arr[0][0] = 10*0 + 0 = 0
    • j=1, arr[0][1] = 10*0 + 1 = 1
    • j=2, arr[0][2] = 10*0 + 2 = 2
    • j=3, arr[0][3] = 10*0 + 3 = 3
    • j=4, arr[0][4] = 10*0 + 4 = 4
  3. During the second iteration (i=1):
    • j=0, arr[1][0] = 10*1 + 0 = 10
    • j=1, arr[1][1] = 10*1 + 1 = 11
    • j=2, arr[1][2] = 10*1 + 2 = 12
    • j=3, arr[1][3] = 10*1 + 3 = 13
    • j=4, arr[1][4] = 10*1 + 4 = 14
  4. Effectively, arr[1] looks like: [10, 11, 12, 13, 14].
  5. Similarly, the rows arr[2] and arr[3] are filled, but those are not used in this particular question.

The critical part of the question is the statement printf("%d", *(arr[1] + 9));.

  • The expression arr[1] points to the start of the second row.
  • arr[1] + 9 moves 9 positions ahead starting from arr[1][0]. This 2D array in C is stored in a row-major order:
    • arr[1][0] (0th offset), arr[1][1] (1st offset), ..., arr[1][4] (4th offset)
    • Continue across rows: arr[2][0] (5th offset), arr[2][1] (6th offset), ..., arr[2][4] (9th offset)
  • Accessing arr[2][4] or *(arr[1] + 9) gives value 24.

Therefore, the program prints 24.

Was this answer helpful?
0