Declare and Initialize a 2D Array in C
In C, a 2D array is an array of arrays, meaning it consists of multiple rows and columns. A 2D array is declared using two sets of square brackets [rows][columns]
. It can be initialized at the time of declaration or later using loops.
In this tutorial, we will cover different ways to declare and initialize a 2D array with examples.
Examples of Declaring and Initializing a 2D Array
1. Declaring and Initializing a 2D Array at the Time of Declaration
In this example, we will declare and initialize a 2D array at the time of its definition.
main.c
</>
Copy
#include <stdio.h>
int main() {
// Declaring and initializing a 2D array
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
// Printing the 2D array elements
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
Explanation:
- We declare a 2D array
matrix[2][3]
with 2 rows and 3 columns. - We initialize the array with predefined values using curly braces
{}
. - Using nested
for
loops, we iterate over each row (i
) and column (j
). - The inner loop prints each element followed by a space, while the outer loop prints a new line after each row.
Output:
1 2 3
4 5 6
2. Initializing a 2D Array Using Loops
In this example, we will initialize a 2D array using nested loops and user input.
main.c
</>
Copy
#include <stdio.h>
int main() {
int matrix[2][2];
// Taking user input to initialize the 2D array
printf("Enter 4 elements for the 2x2 matrix:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
scanf("%d", &matrix[i][j]);
}
}
// Printing the 2D array
printf("The entered matrix is:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
Explanation:
- We declare a 2D array
matrix[2][2]
without initializing it. - Using nested loops, we take user input to fill the array.
- The
scanf()
function reads user input and stores it inmatrix[i][j]
. - Another nested loop is used to print the matrix in the correct format.
Output (Example User Input: 1 2 3 4):
Enter 4 elements for the 2x2 matrix:
1 2
3 4
The entered matrix is:
1 2
3 4
3. Declaring and Initializing a 2D Array with Partial Initialization
In this example, we will see how a 2D array behaves when it is partially initialized.
main.c
</>
Copy
#include <stdio.h>
int main() {
// Partially initialized 2D array
int matrix[3][3] = {
{1, 2},
{3},
{4, 5, 6}
};
// Printing the array
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
Explanation:
- We declare a
3x3
matrix but initialize only some elements. - Missing elements are automatically set to
0
by default. - We use nested loops to print the matrix.
Output:
1 2 0
3 0 0
4 5 6
Conclusion
In this tutorial, we explored different ways to declare and initialize a 2D array in C:
- Direct Initialization: Assigning values while declaring the array.
- Loop Initialization: Taking input using loops and assigning values dynamically.
- Partial Initialization: Initializing only some elements, with others defaulting to zero.