Array initialization with ranges in C

Note
This post is part of a new series of short-form posts titled Today I learned. These posts are intended to be short and more informal—my goal is for you to learn one thing quickly.

Today I learned there is a GNU C extension for designated initializers that makes it possible to initialize a range of elements in an array.

range-example

If you happen to be using GCC (or another compatible compiler that supports this GNU extension, like Clang), you can specify a range ([first ... last]) in the designated initializer list.

Using the example in the diagram above, we can initialize the range of elements [2, 4] to the value 1 and the range [7, 9] to the value 2 using the following syntax:

int a[10] = {[2 ... 4] = 1, [7 ... 9] = 2};

All the other array elements that are not initialized explicitly are implicitly initialized to zero.

As a result, the example above is equivalent to:

int a[10] = {0, 0, 1, 1, 1, 0, 0, 2, 2, 2};