2D arrays in C (Multi-dimensional arrays) language


2D arrays in C Introduction:

We have looked at the one-dimensional arrays in our earlier article, In today’s article, We are going to look at Multi-Dimensional Arrays, In particular 2D arrays in C language – How to create, access, and modify the 2-D arrays in C programming language.

📢 It is recommended to know the array basics to understand the 2D array. Please go through the following article to learn more about one-dimensional arrays.

Multi-Dimensional Array in C:

The multi-dimensional arrays are the array of arrays. Which means each element in the multi-dimensional array is an array.

The multi-dimensional arrays are also called Matrix.

C language supports different dimensional arrays like,

  • One dimensional array or 1-D Array – The One dimensional array is an array of elements/values(Int, float, etc)
  • Two-Dimensional Array or 2-D Array – The Two-dimensional array is an array of 1-D arrays. So each element in the 2D array is a 1-D array.
  • Three-Dimensional Array or 3-D Array – The three-dimensional array is an array of 2-D Arrays. So each element is a 2D array.
  • N-Dimensional Array or N-D Array – The N-Dimensional array is an array of N-1 arrays

Syntax of multi-dimensional Array

We will look at two-dimensional arrays or 2-D arrays in this article.

📢 Related Programs: Arrays Practice Programs in C

Two-Dimensional Arrays or 2-D Arrays in C Language:

The Two Dimensional Arrays or 2-D arrays are collections of one-dimensional arrays ( Array of 1-D Arrays). We can create two-dimensional arrays by specifying the two subscripts [ ].

The two-dimensional array elements are stored in the contiguous memory. We can represent a 2-D array as the Matrix.

The 2-D arrays are used to store and manipulate data in tabular format, with each row representing a different record and each column representing a different field or attribute of that record.

Declaring 2-D Arrays in C:

Syntax of the 2-D array declaration.


  • data_type – The data type of the array elements
  • array_name – Name of the 2-D array
  • row_size – The number of rows in the 2-D array
  • column_size – The number of columns in the 2-D array

📢 The total number of elements in the two-dimensional array is equal to the product of row_size and column_size.

Total Elements in 2-D Array = row_size * column_size

Example of 2-D Array:

Here we created a 2-D array called data which has 3 rows and 4 columns (using two subscripts). The data array stores integer data type values. The data array contains the 12 elements ( 3 * 4)

Accessing the 2-D arrays:

To access the individual elements of the array, We can use the row and column indices.

The element in the 2-D array can be accessed by using the array_name[row_index][column_index], Here the row_index and column_index are the indices of the rows and columns which start from zero(0).

So the First element of the 2-D array will be stored at array_name[0][0]

Here is the representation of the above int data[3][4] array.

Column 0Column 1Column 2Column 3
Row 0 data[0][0] data[0][1] data[0][2] data[0][3]
Row 1 data[1][0] data[1][1] data[1][2] data[1][3]
Row 2 data[2][0] data[2][1] data[2][2] data[2][3]

As specified earlier, We can access 2-D array elements using the row index and column index.

  • To access the element of the First row and third column, We can use the data[0][2]
  • To access the element of the Third row and First column, We can use the data[2][0]

📢The rows and column index of the two-dimensional / 2-D array starts with the index Zero(0).

Initialization of 2D arrays in C:

The two-dimensional arrays can be initialized by specifying the values for the array elements like this.

Here we have created an array called arr, which has 3 Rows and 5 columns. Here we used curly braces to divide and represent each row.

We can create the 2-D arrays by specifying the all elements in single curly braces like below.

The second method is not readable and error-prone. So it is a good idea to stick to the first method.

If you are Initializing the 2-D array, Then the first dimension( row_size) is optional. But you have to specify the second dimension size.

If you don’t specify the second dimension size,

Then you probably will get the compilation error like note: declaration of ‘arr’ as multidimensional array must have bounds for all dimensions except the first

Program to understand the two-dimensional or 2-D arrays in C Programming language:

C Program to Initialize and display the 2-D array.

2-D Array Example Program Explanation:

In this program, we created a two-dimensional array arr. which holds integer elements. The arr[3][5] array contains 3 rows and 5 columns.

To display the two-dimensional array we need to use two for loops ( Similarly, we need 3 loops to print the 3-d array,..)

  • The outer for loop is used to iterate over the rows. So the outer for loop started from the index and went till the 3. – for(i=0; i<3; i++)
  • The Inner For loop is used to iterate over the columns. As the arr array contains 5 columns, The inner for loop will start from goes till 5for(j=0; j<5; j++)
  • At each iteration, Print the array element using row and column indices – arr[i][j]

Program Output:

Compile and Run the program.


As you can see from the above output, The program is displaying all elements of the two-dimensional array (2-D array) with element indices.

Program to Input and Display the 2-D array or Matrix (Read and Print 2-D Array):

C Program to read the 2-D array input from the user and display all elements. We use for loops to iterate over the 2-D array.

Program Explanation:

  • We created a 2-D array data[4][3]. The data array holds 4 rows and 3 columns.
  • Two for loops are used to accept user input. The outer for loop iterates over the rows, while the inner for loop iterates over the columns. At each iteration, obtain the integer data from the user and store it in data[i][j] using the scanf function.
  • To display all array elements of a 2-D Array. We should follow similar steps as input. So, use two for loops to iterate over rows and columns, printing the data[i][j] value at each iteration using printf function.

Program Output:

Compile and run the program using your favorite compiler.

Example Program 2: Sum of all elements of 2D Arrays in C Language:

Write a C Program to calculate the sum of all elements in a 2D Array.

Sum of 2D array elements Program explanation:

The sum of all elements of the 2D array program adds all elements in the 2D array and returns the result.

  • The program creates a values A 2-d array of size [3][3], which means the values array contains three rows and three columns.
  • The next step is to take the user input and update the array. We used two for loops to read the input from the user and updated the array elements.
  • Create a variable called sum and initialize with zero(0). The sum variable holds the sum of array elements.
  • Then we need to go through the all elements of the array and add each element to the sum variable.
  • Display the sum variable on the standard output (console)

Program Output:

Let’s compile and run the program and observe the output.



We have learned about multidimensional arrays in c programming, In particular, we concentrated on 2d arrays in C language. We looked at how to declare, initialize, and process the 2d arrays. Finally, we looked at a couple of examples to make our understanding concrete.

In the next article, We are going to look at How to Pass the Arrays to functions.

Multi-dimensional Arrays Practice Programs (Matrix Programs):


Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

13 Responses

  1. […] 2D arrays in C with Example programs […]

  2. […] looked at the one-dimensional arrays and two-dimensional arrays in our earlier posts, In today’s article, we are going to look at passing arrays to functions in […]

  3. […] 2D arrays in C with Example programs […]

  4. […] 2D-Arrays in C – How to Declare, Access, and Modify 2D arrays […]

  5. […] 2D Arrays in C Language – How to Create and Use 2D Arrays or Matrices in C […]

  6. […] 2D arrays in C with Example programs […]

  7. […] Multi-Dimensional Arrays in C […]

  8. […] Multi-Dimensional Arrays in C […]

  9. […] Multi-dimensional Arrays in C […]

  10. […] Multi-Dimensional Arrays in C […]

  11. […] Multi-dimensional Arrays in C […]

  12. […] Multi-Dimensional Arrays in C […]

  13. […] have looked at the Arrays and multi-dimensional arrays in earlier articles. In today’s article, We will look at the Strings in C programming language. […]

Leave a Reply