# Codzify - Two dimensional (2D) arrays in C programming with example

3 min 42 sec read Basic As we discussed in our previous article on One Dimensional array, data was organized linearly in only one direction.

`int a = {1,2,3,4,5};`

Two Dimensional array provides the way to store data in the form of grids and tables. You can think of the two-dimensional array as an array of one-dimensional array. From the figure, you might notice that there are three one dimensional arrays in three rows.

## Syntax:

`datatype array_Name[row_size][column_size]`

Two Dimensional arrays are specified using two subscripts where the first subscript denotes the row and the second denotes the column.

Suppose, you want to store marks of two students in three subjects then two-dimensional array can be declared like this,

`marks` ## Initializing Two-dimensional Arrays

2d arrays are initialized using the same syntax as one dimensional array,

`int marks = {88,87,76,88,76,56}`

As you see, for marks dimensional array there are 6 elements . In generalize term, we can say for arr[m][n] dimensional array, there will be m X n number of elements in an array.

You can think about the given initialized array of marks like this also,

`int marks = {{88,87,76}, {88,76,56}}`

Initialize the entire two dimensional array to zeros,

` int marks = 0;`

The individual elements of two dimensional array can be initialized using the assignment operator,

`marks = 87;`

## Simple program to understand the Two dimensional array in action

Program to print the elements of 2d array in C,

```#include <stdio.h>
int main()
{
int arr = {10,11,12,13};
int i,j;
for(i = 0; i<2; i++)
{
printf("
");
for(j = 0; j<2; j++)
{
printf("%d	", arr[i][j]);
}
}
return 0;
}
}
```

## Output

```10   11
12   13
```

## How two dimensional arrays are stored in memory ?

There are two ways of storing the two-dimensional array in memory.

• Row major order
• Column major order

## Row-major order

The elements in row-major order are stored row by row in memory. Elements of the first row will store before the second and next one. You can also compute the address of an element in row-major order by the following formula,

`Address(A[I][J]) = Base_Address + w(N(I - 1) + (J - 1))`

where w is the number of bytes required to store an element, N is the number of columns, M is the number of rows, and I and J are the subscripts of the array element.

## Column-major order

The elements in column-major order are stored column by column in memory. Elements of the first column will store before the second and next one. You can also compute the address of an element in column-major order by the following formula,

`Address(A[I][J]) = Base_Address + w(M(J - 1) + (I - 1))`

where w is the number of bytes required to store an element, N is the number of columns, M is the number of rows, and I and J are the subscripts of the array element.

How was the tutorial? Nice. Right?

## What is your Interview Score?

Test your skillset with the curated questions created by experts around the globe.

Book a free test slot. Now !.

It usually takes 30 mins for an online test and this test will be MCQ based. Get detailed analytics based on your test and recommedations with personalised roadmaps.

Book a test slot. Now!

## Have questions about the trial coding class?

Chat with our experts to discuss

Connect on WhatsApp

## Recommended tutorials

#online compiler for c     #python for programming

#dfs and bfs algorithm     #programming with c language

#storageclass in C    #listcomprehension in python