25.5.1 Array Literals

Array literals are constructed using the following syntax:

[exp,exp...]

where exp is an arbitrary expression.

For example, [1,2,3] constructs an array of three signed 32-bit integers. Likewise, ['a','b','c'] constructs an array of three unsigned 8-bit integers (characters). For convenience, a trailing comma is accepted but not required.

The type of the array literal is inferred from the type of its elements. Accordingly, all the elements in an array literal must be of the same type. Examples of invalid array literals, that will raise a compilation-time error if evaluated, are:

[1,2u,3]
[1,0xffff_ffff,3]
['a',"b",'c']

Array literals must contain at least one element. Accordingly, [] is not a valid array literal.

A set of suffixes can be used to construct array literals of integers of certain types. L or l is for array of 64-bit integers. H or h is for array of 16-bit integers, B or b is for array of 8-bit integers, n or N is for array of 4-bit integers and t or T is for array of 1-bit integers.

[1,2,3]UB == [1UB,2UB,3UB]
[1,2]UL == [1UL,2UL]
[-1,-2,-3]UB == [0xffUB,0xfeUB,0xfdUB]

This is how a 3x3 matrix could be constructed using an array of arrays:

[[1,2,3],[4,5,6],[7,8,9],]

It is possible to refer to specific elements when constructing array literals. For example, [1,2,3,.[3] = 4] denotes the same array as [1,2,3,4].

This allows creating arrays without having to specify all its elements; every unspecified element takes the value of the first specified element to its right. For example, [.[2] = 2] denotes the same array as [2,2,2].

Note that an array element can be referenced more than once. When that happens, the final value of the element is the last specified. For example, [1,2,3,.[1]=10] denotes the array [1,10,3].