Two dimensional array in C | Initialization of Two dimensional array in C

Preview
You must first complete Introduction to Array in C | One dimensional array in C before viewing this Lesson

Two dimensional array in C

Two dimensional array is an array whose elements are represented in the form of rows and columns. The syntax for declaring a two dimensional array is:

<Type> Array_name[ROW][COL];

Type refers to data type of array like int, float, char etc.

Array_name  is the name of array as defined by the programmer.

ROW specifies number of rows in the two dimensional array.

COL specifies the number of columns in the two dimensional array.

Example

int A[2][2];

Array A[2][2] contains 4 elements referred as A[0][0], A[0][1], Arr[1][0], and A[1][1].



Program for two dimensional array.
#include<stdio.h>
int main()
{
int arr[2][2];
arr[0][0]=1;
arr[0][1]=2;
arr[1][0]=3;
arr[1][1]=4;
printf(“\n%d”,arr[0][0]);
printf(“\n%d”,arr[0][1]);
printf(“\n%d”,arr[1][0]);
printf(“\n%d”,arr[1][1]);
return(0);
}
Output
1
2
3
4



Initialization of a Two dimensional array in C

Syntax to initialize a two dimensional array is:

<Type> Array_name[ROW][COL]={LIST};

LIST is set of values stored in a two dimensional array. Values should be separated with commas.

Various ways to initialize a two dimensional array are:

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

int arr2[3][2]={3,4,

                 5,6,

                 8,9

                 };

int arr3[3][3]={{6,9,8},

                 {2,6,8},

                 {4,9,3}

                 };


Program to initialize a two dimensional array.
#include<stdio.h>
int main()
{
int arr[2][2]={10,20,30,40};
printf(“\n%d”,arr[0][0]);
printf(“\n%d”,arr[0][1]);
printf(“\n%d”,arr[1][0]);
printf(“\n%d”,arr[1][1]);
return(0);
}
Output
10
20
30
40



Best Books of C





Lesson tags: initialization of two dimensional array in c, matrix in c, reading two dimensional array in c, two dimensional array in c
Back to: C Programming Language