# C Programming Two Dimensional Array with Examples

Two-dimensional array in c programming; In this tutorial, i am going to show you what is two Dimensional Array in C and how to use two dimensional array in c.

## Two Dimensional Array in C with Examples

Here, i will teach you what is two dimensional array, how to declare, initialize, and use of two dimensional with the help of examples: is as follows:

• Two Dimensional Array Definition in C
• Two Dimensional Array Declaration in C
• Initialization of Two Dimensional Array
• Example 1 – Two Dimensional Array in C
• Example 2 – Storing elements in a matrix and printing it using 2d array in C
• Example 3 – Find sum of all elements in a 2D (Two Dimensional) Array

### Two Dimensional Array Definition in C

In c programming; two-dimensional arrays are stored as arrays of arrays. It’s is also known as matrix array.

Note that:- A matrix can be represented as a table of rows and columns.

### Two Dimensional Array Declaration in C

You can use the following syntax to declare a two-dimensional array in the c programming language; as shown below:

```data_type array_name[rows][columns];
```

Let’s see the following example for how to declare two-dimensional array in c programming; as shown below:

```int twodimen;
```

Here, 4 is the number of rows, and 3 is the number of columns.

### Initialization of Two Dimensional Array

To initialize an array in c by using the index of each element. See the following easy way to initialize array in c programming; as shown below:

```int disp = { 10, 11, 12, 13, 14, 15, 16, 17};
```

### Example 1 – Two Dimensional Array in C

```#include<stdio.h>
int main(){
int i=0,j=0;
int arr={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
//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
return 0;
}    ```

Output:

```arr = 1
arr = 2
arr = 3
arr = 2
arr = 3
arr = 4
arr = 3
arr = 4
arr = 5
arr = 4
arr = 5
arr = 6```

### Example 2 – Storing elements in a matrix and printing it using 2d array in C

```#include <stdio.h>
void main ()
{
int arr,i,j;
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
printf("Enter a[%d][%d]: ",i,j);
scanf("%d",&arr[i][j]);
}
}
printf("\n printing the elements ....\n");
for(i=0;i<3;i++)
{
printf("\n");
for (j=0;j<3;j++)
{
printf("%d\t",arr[i][j]);
}
}

return 0;
}    ```

Output of the above c program; as shown below:

```Enter a: 12
Enter a: 45
Enter a: 63
Enter a: 45
Enter a: 25
Enter a: 12
Enter a: 74
Enter a: 85
Enter a: 65

printing the elements ....

12	45	63
45	25	12
74	85	65	```

### Example 3 – Find sum of all elements in a 2D (Two Dimensional) Array

```Enter number of Rows :2
Enter number of Cols :2
Enter matrix elements :
Enter element [1,1] : 1
Enter element [1,2] : 2
Enter element [2,1] : 3
Enter element [2,2] : 4
SUM of all elements : 10 ```

Output of the above c program; as shown below:

```Enter number of Rows :2
Enter number of Cols :2
Enter matrix elements :
Enter element [1,1] : 1
Enter element [1,2] : 2
Enter element [2,1] : 3
Enter element [2,2] : 4
SUM of all elements : 10 ```

Categories C