Two Dimensional Array in C

The Two Dimensional Array in C language is nothing but an Array of Arrays. If the data is linear, we can use the One Dimensional Array. However, to work with multi-level data, we have to use the Multi-Dimensional Array.

Two Dimensional Array in C is the simplest form of Multi-Dimensional Array. In C Two Dimensional Array, data is stored in row and column wise. We can access the record using both the row index and column index (like an Excel File).

Declaration of Two Dimensional Array in C

The basic syntax or, the declaration of two dimensional array in C Programming is as shown below:

Data_Type Array_Name[Row_Size][Column_Size]
  • Data_type: This will decide the type of elements will accept by two dimensional array in C. For example, If we want to store integer values then we declare the Data Type as int, If we want to store Float values then we declare the Data Type as float etc
  • Array_Name: This is the name you want to give it to this C two dimensional array. For example students, age, marks, employees etc
  • Row_Size: Number of Row elements an array can store. For example, Row_Size =10, then the array will have 10 rows.
  • Column_Size: Number of Column elements an array can store. For example, Column_Size = 8, the array will have 8 Columns.

For Example,

int Employees[4][3];

  1. Here, we used int as the data type to declare an array. So, above C two dimensional array will accept only integers. If you try to add float values, it will through an error.
  2. Employees – the name of the Two Dimensional Array in C
  3. The Row size of an Array is 4. It means Employees array will only accept four integer values as rows.
    • If we try to store more than 4 values, then it will throw an error.
    • We can store less than 4. For Example, If we store two integer values, then the remaining 2 values will assign to the default value (Which is 0).
  4. The Column size of an Array is 3. It means Employees array will only accept 3 integer values as columns.
    • If we try to store more than 3, it will throw an error.
    • We can store less than 3. For Example, If we store 1 integer value, the remaining 2 values will be assigned to the default value (Which is 0).

Two Dimensional Array in C Initialization

We can initialize the C Two Dimensional Array in multiple ways. Before this, Please refer to the Array and Multi-Dimensional Array articles in C Programming

C Two Dimensional Array First Approach

int Employees[4][3] = { 10, 20, 30, 15, 25, 35, 22, 44, 66, 33, 55, 77 };

The first three elements will be 1st row, the second three elements will be 2nd row, the next 3 elements will be 3rd row, and the last 3 elements will be 4th row. Here we divided them into 3 because our column size = 3.

The above C two dimensional array statement can also be written as

int Employees[4][3] = { {10, 20, 30}, {15, 25, 35}, {22, 44, 66}, {33, 55, 77} };

Here, We surrounded each row with curly braces ({}). It is always good practice to use the curly braces to separate the rows.

Second Approach for Two Dimensional Array in C

int Employees[ ][ ] = { {10, 20, 30}, {15, 25, 35}, {22, 44, 66}, {33, 55, 77} };

Here, We haven’t mentioned the row size and column size. However, the compiler is intelligent enough to calculate the size by checking the number of elements inside the row and column.

We can also write this two dimensional array in c as

int Employees[ ][3] = { {10, 20, 30}, {15, 25, 35}, {22, 44, 66}, {33, 55, 77} };

Third Approach of Two Dimensional Array in C

int Employees[2][3] = { {4},
                        {6, 7} 
                      };

Here, we declared Employees array with row size =2 and column size = 3, but we only assigned 1 column in the 1st row and 2 columns in the 2nd row. In these situations, the remaining values will assign to default values (0 in this case).

The above array will be:

Employees[2][3] = { {4, 0, 0},
                    {6, 7, 0} 
                  };  
//It means

Employees[0][0] = 4

Employees[0][1] = 0

Employees[0][2] = 0

Employees[1][0] = 6

Employees[1][1] = 7

Employees[1][2] = 0

C Two Dimensional Array Fourth Approach

The above three ways of initializing two dimensional array in C are good to store a small number of elements into the array. What if we want to store 100 rows or 50 column values. It will be a nightmare to add all of them using any of the approaches mentioned above. To resolve this, we can use the For Loop in C Programming concept here:

int rows, columns, Employees[100][50];

for (rows =0; rows < 100 ; rows++)
 {
   for (columns =0; columns < 50; columns++)
    {
      Employees[rows][columns] = rows + columns;
 }

Say,

Employees[3][4] = 3+4 = 7

Access C Two Dimensional Array Elements

We can access the Two Dimensional Array in C Programming elements using indexes. Using the index, we can access or alter/change each element present in the array separately. Index value starts at 0 and ends at n-1, where n is the size of a row or column.

For example, if an Array_name[8][5] will stores 8 row elements and 5 column elements. To access or alter 1st value use Array_name[0][0], to access or alter 2nd row 3rd column value then use Array_name[1][2] and to access the 8th row 5th column then use Array_name[7][4]. Lets see the example of a C Two Dimensional Array for better understanding:

int Employees[4][3] = { {10, 20, 30},
                        {15, 25, 35},
                        {22, 44, 66},
                        {33, 55, 77}
                      };

//To Access the values in the Employees[4][3] array

Printf("%d", Employees[0][0]) = 10

Printf("%d", Employees[0][1]) = 20

Printf("%d", Employees[0][2]) = 30

Printf("%d", Employees[1][0]) = 15

Printf("%d", Employees[1][1]) = 25

Printf("%d", Employees[1][2]) = 35

Printf("%d", Employees[2][0]) = 22

Printf("%d", Employees[2][1]) = 44

Printf("%d", Employees[2][2]) = 66

Printf("%d", Employees[3][0]) = 33

Printf("%d", Employees[3][1]) = 55

Printf("%d", Employees[3][2]) = 77


//To Alter the values in the Employees[4][3] array

Employees[2][1] = 98; - It will change the value of Employees[2][1] from 44 to 98

For a large number of rows and columns, we can access them using For Loop. Let’s use the above array Employees[4][3]

int rows, columns;

for (rows = 0; rows < 4; rows++)
 {
  for (columns =0; columns < 3; columns++)
   {
     Printf("%d", Employees[rows][columns]);
   }
 }

Two Dimensional Array in C Example

In this C two dimensional array program, We will declare 2 Two dimensional arrays and initialize them with some values. Next, we will declare one more Two dimensional array to store the sum those 2 arrays.

/* Two Dimensional Array in C Example */
#include<stdio.h>

int main()
{
  int a[2][3] = { {15, 25, 35}, {45, 55, 65} };
  int b[2][3] = { {12, 22, 32}, {55, 25, 85} };
  int rows, columns, Sum[2][3];
  
  for(rows = 0; rows < 2; rows++)
   {
    for(columns = 0; columns < 3;columns++)
     {
      Sum[rows][columns] = a[rows][columns] + b[rows][columns];  
     }
   }
  
  printf("\n Sum Of those Two Arrays are:\n");
  for(rows = 0; rows < 2; rows++)
   {
    for(columns = 0; columns < 3; columns++)
     {
      printf("%d, ", Sum[rows][columns]);
     }
   }
  
  return 0;
}
Two Dimensional Array in C

Let us see the execution of the Two Dimensional Array in iteration wise

  for(rows = 0; rows < 2; rows++)
   {
    for(columns = 0; columns < 3;columns++)
     {
      Sum[rows][columns] = a[rows][columns] + b[rows][columns];  
     }
   }

Row First Iteration

The value of the row will be 0, and the condition (rows < 2) is True. So, it will enter into second for loop

Column First Iteration
The value of the column will be 0, and the condition (columns < 2) is True. So, it will start executing the statements inside the loop until the condition fails.
Sum[rows][columns] = a[rows][columns] + b[rows][columns];
Sum[0][0] = a[0][0] + b[0][0];
Sum[0][0] = 15 + 12;
Sum[0][0] = 27;

Column Second Iteration
The value of column will be 1, and the condition (columns < 3) is True. Since we didn’t exit from the inner loop (Columns loop), the row value will be 0
Sum[0][1]= a[0][1] + b[0][1];
Sum[0][1]= 25 + 22;
Sum[0][1]= 47;

Column 3rd Iteration
columns = 2, and the condition (2 < 3) is True.
Sum[0][2] = a[0][2] + b[0][2];
Sum[0][2] = 35 + 32;
Sum[0][2] = 67;

After incrementing, the value of columns will be 3, and the condition (columns < 3) will fail. So, it will exit from the loop.

Now, the value of the rows will increment, and starts the second row iteration of two dimensional array in c

Row Second Iteration
The value of the row will be 1, and the condition (rows < 2) is True. So, it will enter into second for loop

Column First Iteration
The value of the column will be 0, and the condition (columns < 3) is True.
Sum[1][0] = a[1][0] + b[1][0];
Sum[1][0] = 45 + 55;
Sum[1][0] = 100;

Column Second Iteration
column = 1, and the condition (columns < 3) is True.
Sum[0][1]= a[1][1] + b[1][1];
Sum[0][1]= 55 + 25;
Sum[0][1]= 80;

Column 3rd Iteration
columns = 2, and the condition (columns < 3) is True.
Sum[1][2] = a[1][2] + b[1][2];
Sum[1][2] = 65 + 85;
Sum[1][2] = 150;

After incrementing, the value of columns will be 3, and the condition (columns < 3) will fail. So it will exit from the loop.

Now, the value of the rows will increment, it means rows =2. Condition (rows < 2) will fail, so it will exit from the loop.

The next for loop in this C Two Dimensional Array example,

for(rows = 0; rows < 2; rows++)
 {
   for(columns = 0; columns < 3; columns++)
    {
      printf("%d, ", Sum[rows][columns]);
    }
 }

This for loop in two dimensional array program will traverse as we explained above. However, instead of summing, it will display the values one by one with comma separation using the printf statement inside them.

The final output of the Sum array is:
Sum[2][3] = { {27, 47, 67}, {100, 80, 150} };

Comments are closed.