📊⚡Beginner
Print a 2D Array
Display 2D array elements
A 2D array (matrix) is an array of arrays. It stores elements in rows and columns. To print a 2D array, we use nested loops - outer loop for rows, inner loop for columns.
📊 2D Array Structure
[0][0][0][1][0][2][0][3][1][0][1][1][1][2][1][3][2][0][2][1][2][2][2][3]
C Program to Print a 2D Array
print_2d_array.c
C
1#include <stdio.h>23int main() {4 int rows = 3, cols = 4;5 6 // Initialize 2D array7 int arr[3][4] = {8 {1, 2, 3, 4},9 {5, 6, 7, 8},10 {9, 10, 11, 12}11 };12 13 printf("2D Array Elements:\n");14 15 // Nested loops to print16 for (int i = 0; i < rows; i++) {17 for (int j = 0; j < cols; j++) {18 printf("%4d ", arr[i][j]);19 }20 printf("\n"); // New line after each row21 }22 23 return 0;24}Output
2D Array Elements:
1 2 3 4
5 6 7 8
9 10 11 12
📖 Code Explanation
| Code | Explanation |
|---|---|
| int arr[3][4] | Declare 2D array with 3 rows and 4 columns |
| for (i = 0; i < rows) | Outer loop iterates through rows |
| for (j = 0; j < cols) | Inner loop iterates through columns |
| arr[i][j] | Access element at row i, column j |
🎯 Key Takeaways
✓2D arrays: arr[rows][cols]
✓Use nested loops for traversal
✓First index = row, second = column
✓Memory is stored row by row
Related Examples
Want to Learn More?
Explore our comprehensive tutorials for in-depth explanations of C programming concepts.
Browse Tutorials