You are on page 1of 2

A 2D array is like a matrix and has a row and a column of elements 

( Although in memory these are stored in


contiguous memory locations)

How to declare a 2D Array in C?


A 2D array needs to be declared so that the compiler gets to know what type
of data is being stored in the array.

Similar to 1D array, a 2D array can also be declared as


an int, char, float, double, etc. Here is how we declare a 2D array(here
integer array):

2D array declaration
datatype arrayVariableName[number of rows] [number of columns]
int num[10][5];

Here is a simple program of 2D array which adds two arrays and stores the
result in another array. One array has already been initialized and the other
one will have data input by the user.

#include <stdio.h>

int main() {

// we initialize the first array and the second array


will have user input

// values

int a[3][3] = {

{1, 34, 5}, {7, 0, 15}, {23, 4, 6}}; // first array


initialization
int b[3][3], c[3][3], i, j;

printf("Enter values in the 3x3 array:\n");

for (i = 0; i < 3; i++) { // outer loop for rows

for (j = 0; j < 3; j++) { // inner loop for columns

scanf("%d", &b[i][j]);

c[i][j] = a[i][j] + b[i][j]; // summing up the values


of the two arrays

// displaying the array elements after summing up

for (i = 0; i < 3; i++) {

for (j = 0; j < 3; j++) {

printf("%d ", c[i][j]);

printf("\n");

return 0;

}
Output:-
Enter values in the 3x3 array:
30
4
17
6
9
14
20
0
6

31 38 22
13 9 29
43 4 12

You might also like