Two Dimensional Array in C Program

06-11-17 Course- C

The two dimensional array in C language is represented in the form of rows and columns, also known as matrix. It is also known as array of arraysor list of arrays.

Two-dimensional, three-dimensional or other dimensional arrays are also known as multi-functional arrays.

Declaration of two dimensional Array in C

We can declare an array in the c language in the following way.


data_type array_name[size1][size2];  

A simple example to declare two dimensional array is given below.


int twodimen[4][3];  

Here, 4 is the row number and 3 is the column number.

Initialization of 2D Array in C

At the time of declaration, one way to start a two-dimensional array is given below.


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

Two dimensional array example in C


#include <stdio.h>    
#include <conio.h>    
void main(){    
int i=0,j=0;  
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};  
clrscr();    
  
//traversing 2D array  
for(i=0;i<4;i++){  
 for(j=0;j<3;j++){  
   printf("arr[%d] [%d] = %d \n",i,j,arr[i][j]);  
 }//end of j  
}//end of i  
  
getch();    
}    

Output


arr[0][0] = 1
arr[0][1] = 2
arr[0][2] = 3
arr[1][0] = 2
arr[1][1] = 3
arr[1][2] = 4
arr[2][0] = 3
arr[2][1] = 4
arr[2][2] = 5
arr[3][0] = 4
arr[3][1] = 5
arr[3][2] = 6