---
title: 5.2 Array Initialization
description: "Array initialization assigns initial values to array elements when the array is declared. C supports initializing all elements, initializing selected elements, and allowing the compiler to determine the array size from the initializer. ``` #include int main(void) { int numbers[5]…"
url: "https://edcret.in/docs/c-programming/arrays/5-2-array-initialization/"
updated: "2026-10-27"
category: C Programming / Arrays
---

# 5.2 Array Initialization

Array initialization assigns initial values to array elements when the array is declared. C supports initializing all elements, initializing selected elements, and allowing the compiler to determine the array size from the initializer.

```
#include <stdio.h>

int main(void)
{
    int numbers[5] = {10, 20, 30, 40, 50};
    int values[5] = {1, 2};
    int scores[] = {75, 85, 95};

    for (int i = 0; i < 5; i++)
    {
        printf("%d ", numbers[i]);
    }

    printf("n");

    for (int i = 0; i < 5; i++)
    {
        printf("%d ", values[i]);
    }

    printf("n");

    for (int i = 0; i < 3; i++)
    {
        printf("%d ", scores[i]);
    }

    printf("n");

    return 0;
}
```

### Example Output

```
10 20 30 40 50
1 2 0 0 0
75 85 95
```

### Explanation

The declaration:

```
int numbers[5] = {10, 20, 30, 40, 50};
```

initializes all five elements with the specified values.

When an array is partially initialized, the remaining elements are initialized to zero:

```
int values[5] = {1, 2};
```

The resulting elements are `{1, 2, 0, 0, 0}`.

When the array size is omitted, the compiler determines the number of elements from the initializer:

```
int scores[] = {75, 85, 95};
```

This declaration creates an array containing three elements.

### Array Initialization Forms

| Declaration | Result |
| --- | --- |
| `int a[3] = {1, 2, 3};` | All elements initialized explicitly |
| `int a[3] = {1};` | First element is `1`; remaining elements are `0` |
| `int a[] = {1, 2, 3};` | Size inferred as `3` |
| `int a[3] = {0};` | All elements initialized to `0` |
| `int a[5] = {1, 2, 3, 4, 5};` | All five elements initialized |

Designated initializers can initialize specific indices:

```
int numbers[5] = {
    [0] = 10,
    [3] = 40
};
```

The resulting array is `{10, 0, 0, 40, 0}`. Designated initializers are supported in C99 and later.

Array initialization occurs as part of the declaration. Ordinary arrays cannot be initialized later using an assignment-style initializer; individual elements must be assigned separately.
