---
title: "Compile time initialization of arrays"
subject: "Computer Programming"
chapter: 7
key: "semester_1/computer_programming:compile-time-init-2d-array"
source: https://www.bctengineer.com/q/semester_1/computer_programming/compile-time-init-2d-array
site: BCT Engineering
---

# Compile time initialization of arrays

Computer Programming · Chapter 7: Arrays and Strings

- Asked in: 2072 Kartik, 2+3 marks
- Video: [Initializing an Array](https://www.youtube.com/watch?v=GtcRReso9Xk) — Neso Academy
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/compile-time-init-2d-array
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-7

## Question

Explain with an example for compile time initialization of 2D array. Describe how compiler manages according to the number of initializers and size of an array given by a user in case of 1D array.

## Short answer

Compile-time initialization of 2D: int m[2][3] = {{1,2,3},{4,5,6}}; row-wise; column size mandatory. For 1D, if size given and fewer initializers, remainder set to 0; if size omitted, compiler counts initializers; excess initializers cause error.

## Answer

#### Compile time initialisation of a 2D array
The values are given in the declaration, so the compiler stores them when the program is compiled/loaded, and no input is needed at run time.

```c
int m[2][3] = {{1, 2, 3}, {4, 5, 6}};
```
Each inner brace is one row. `m[0][1]` is 2 and `m[1][2]` is 6. It can also be written in one list: `int m[2][3] = {1, 2, 3, 4, 5, 6};`, filled row by row. The row size may be omitted, but the column size is compulsory: `int n[][2] = {{1, 2}, {3, 4}, {5, 6}};` gives 3 rows.

#### How the compiler handles the initialiser count and array size (1D)
1. **Size given and initialisers equal**: all elements get the given values.
2. **Fewer initialisers than size**: the given values go to the first elements and the remaining elements are set to 0, e.g. `int b[6] = {1, 2};` gives 1 2 0 0 0 0.
3. **Size omitted**: the compiler counts the initialisers and sets the size, e.g. `int a[] = {5, 10, 15, 20};` creates 4 elements.
4. **More initialisers than size**: error/warning (excess elements); not allowed.

```c
#include <stdio.h>

int main(void)
{
    int m[2][3] = {{1, 2, 3}, {4, 5, 6}};   /* 2D, fully initialised */
    int a[]  = {5, 10, 15, 20};             /* size omitted -> 4 */
    int b[6] = {1, 2};                      /* rest become 0 */
    int n[][2] = {{1, 2}, {3, 4}, {5, 6}};  /* only first size may be omitted */

    printf("sizeof a = %zu bytes (%zu elements)\n", sizeof a, sizeof a / sizeof a[0]);
    printf("b: ");
    for (int i = 0; i < 6; i++)
        printf("%d ", b[i]);
    printf("\nm[1][2] = %d, rows of n = %zu\n", m[1][2], sizeof n / sizeof n[0]);
    return 0;
}
```

Output:

```text
sizeof a = 16 bytes (4 elements)
b: 1 2 0 0 0 0 
m[1][2] = 6, rows of n = 3
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What does compile time initialisation of a 2D array mean?
   A. Values are given in the declaration, so no input is needed at run time
   B. Values are read with scanf
   C. Values are zero always
   D. Values are given in the loop
   Answer: A. The compiler stores the given values. (Key point: Compile-time initialisation)

2. In a 2D array declaration which size may be omitted?
   A. The column size only
   B. Both
   C. The row size only
   D. Neither
   Answer: C. int n[][2] = {{1,2},{3,4},{5,6}} gives 3 rows; column size is compulsory. (Key point: Omitting sizes)

3. What happens in int b[6] = {1, 2};?
   A. The remaining elements are set to 0
   B. The remaining elements are garbage
   C. A compile error occurs
   D. The size becomes 2
   Answer: A. The result is 1 2 0 0 0 0. (Key point: Fewer initialisers)

4. What is the size of int a[] = {5, 10, 15, 20};?
   A. 5 elements
   B. 20 elements
   C. 4 elements (16 bytes with 4-byte int)
   D. It is an error
   Answer: C. The compiler counts the initialisers. (Key point: Size omitted)

## Related in Computer Programming

- [Creating 1D and 2D arrays](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aone-two-dim-arrays-created) — Computer Programming, chapter 7, asked 2×
- [Array overflow/underflow; add two arrays](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aarray-overflow-underflow-add) — Computer Programming, chapter 7
- [Pass 2D array; largest and smallest](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apass-2d-array-largest-smallest) — Computer Programming, chapter 7
- [Correct program: case array and average](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acorrect-program-case-array) — Computer Programming, chapter 3
- [Insert number at position in array](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-insert-number-in-array) — Computer Programming, chapter 11
- [Pointers in pass by reference; 2D array](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-passing-by-reference-2d) — Computer Programming, chapter 9

## Related in other subjects

- [Selecting size and number of lifts](https://www.bctengineer.com/question.md?key=semester_6%2Findustrial_power_distribution_and_illumination%3Alift-size-number-selection) — Industrial Power Distribution and Illumination, chapter 5
- [Number of persons from reverberation time](https://www.bctengineer.com/question.md?key=semester_1%2Fengineering_physics%3Areverberation-persons-present-hall) — Engineering Physics, chapter 3, asked 2×
- [Number and size of RSF, 1,20,000 population](https://www.bctengineer.com/question.md?key=semester_5%2Fwater_supply_engineering%3Arsf-b78) — Water Supply Engineering, chapter 6
- [Antenna arrays and two-element expression](https://www.bctengineer.com/question.md?key=semester_6%2Fpropagation_and_antenna%3Adefine-array-two-isotropic) — Propagation and Antenna, chapter 2
