Which of the following is a valid 2D array initialization in C?
2025
Which of the following is a valid 2D array initialization in C?
- A.
int arr[][] = {{1, 2}, {3, 4}};
- B.
int arr[2,2] = {1, 2, 3, 4};
- C.
int arr[2][2] = {{1, 2}, {3, 4}};
- D.
int arr[2][2] = (1, 2, 3, 4);
Attempted by 64 students.
Show answer & explanation
Correct answer: C
Specifying both dimensions arr[rows][columns] and using nested braces {} is the clearest way to initialize the structure:
int arr[2][2] = {{1, 2}, {3, 4}};
Omitting the First Dimension:** C allows you to leave the *row* dimension empty if you provide an initializer list, because it can deduce the number of rows based on the data. However, the *column* size remains strictly mandatory:
```c
int arr[][2] = {{1, 2}, {3, 4}}; // Also valid!
You can also initialize a 2D array using a single flat list of elements inside curly braces, as long as the column dimension is specified:
int arr[2][2] = {1, 2, 3, 4}; // Also valid (implicitly mapped row-by-row)