Two Dimensional Array in C Programming
Two Dimensional Array in C Programming
Introduction to Two Dimensional Array in C Programming
A two dimensional array in C programming is used to store data in rows and columns. It is also called a matrix or table.
Two dimensional arrays in C programming are useful for representing grids, matrices, and tabular data.
What is Two Dimensional Array
A two dimensional array is an array of arrays. It stores elements in a row and column format.
Syntax of Two Dimensional Array
Example
This creates an array with 2 rows and 3 columns.
Initialization of Two Dimensional Array
Method 1: Full Initialization
{1, 2, 3},
{4, 5, 6}
};
Method 2: Inline Initialization
Method 3: Without Row Size
{1, 2, 3},
{4, 5, 6}
};
Accessing Elements in Two Dimensional Array
Elements are accessed using row and column index.
Example
int main() {
int arr[2][3] = {{10, 20, 30}, {40, 50, 60}};
printf(“%d\n”, arr[0][0]);
printf(“%d\n”, arr[1][2]);
return 0;
}
Using Nested Loops with Two Dimensional Array
int main() {
int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
int i, j;
for(i = 0; i < 2; i++) {
for(j = 0; j < 3; j++) {
printf(“%d “, arr[i][j]);
}
printf(“\n”);
}
return 0;
}
Input and Output in Two Dimensional Array
int main() {
int arr[2][2], i, j;
for(i = 0; i < 2; i++) {
for(j = 0; j < 2; j++) {
printf(“Enter value: “);
scanf(“%d”, &arr[i][j]);
}
}
printf(“Matrix is:\n”);
for(i = 0; i < 2; i++) {
for(j = 0; j < 2; j++) {
printf(“%d “, arr[i][j]);
}
printf(“\n”);
}
return 0;
}
Example: Matrix Addition
int main() {
int a[2][2] = {{1, 2}, {3, 4}};
int b[2][2] = {{5, 6}, {7, 8}};
int sum[2][2], i, j;
for(i = 0; i < 2; i++) {
for(j = 0; j < 2; j++) {
sum[i][j] = a[i][j] + b[i][j];
}
}
printf(“Sum of matrices:\n”);
for(i = 0; i < 2; i++) {
for(j = 0; j < 2; j++) {
printf(“%d “, sum[i][j]);
}
printf(“\n”);
}
return 0;
}
Advantages of Two Dimensional Array
Key Benefits
- Stores data in table format
- Useful for matrix operations
- Efficient data representation
- Works with nested loops
Limitations of Two Dimensional Array
Key Points
- Fixed size
- Uses more memory
- Complex compared to 1D arrays
When to Use Two Dimensional Array
Use two dimensional array in C programming when:
- Working with matrices
- Handling tabular data
- Using rows and columns
Start Learning C Programming
Practice two dimensional array programs to understand matrix operations in C programming.
Summary
Two dimensional array in C programming stores data in rows and columns. It is useful for matrices and structured data handling.
FAQs
What is two dimensional array in C programming?
It is an array that stores data in rows and columns.
How to access elements in 2D array?
Using row and column index like arr[i][j].
What is matrix in C programming?
It is a two dimensional array.
Can we use loops in 2D array?
Yes, nested loops are used.



