C Programming

Two Dimensional Array in C

The two-dimensional array can be defined as an array of arrays.A two-dimensional array can be used to represent a matrix, a table or board games (Sudoku). The row and column positions are given as successive indices. When you declare a variable of such an array, use a pair of square brackets for each dimension.

Declaration of Two Dimensional Array :

The syntax of 2D array declaration is given below.

Data_type Array_name[Rows][Columns];

Consider the following example.

int Twodimen[4][3];

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

Initilization of Two Dimensional Array :

In the 1D array, we don’t need to specify the size of the array if the declaration and initialization are being done simultaneously. However, this will not work with 2D arrays. In 2D array, we will have to define at least the second dimension of the array. The two-dimensional array can be declared and defined in the following way.

int arr[4][3]={{1,3,4},{6,1,5},{2,9,8},{4,6,7}};

Example of Two Dimensional Array in C:

To print all the elements of a two dimension array we use a doubly-nested for-loop. In the following example, there are 3 rows and 2 columns.

#include <stdio.h>
main()
{
  int row,col;
  int table[3][2] = { {10, 22}, {33, 44}, {45, 78} };
  for (row = 0; row < 3; row++)
  {
  for (col = 0; col < 2; col++)
  {
  printf("%d\t",table[row][col]);
  }
  printf("\n");
  } 
}

Output :

Premium E-Books & Study Materials.

Access university-specific notes and programming guides. Download instantly to start studying.

Share Article :

TopperWorld
Prime E-Books.

Access complete study guides for programming and core CS concepts.

Recent Articles :

What Jobs Will AI Replace: Which Careers Will Grow in the Next 5 Years?
Java Most Asked Interview Question and Answer
Top 10 AI companies in India
How Generative AI is Transforming Programming
Top GITHUB Interview Questions and Answers