Is an empty initializer list valid C code?

According to the C99 standard, array creation with an empty initializer list is forbidden. In a previous answer, you can see that grammar does not describe this case.


But what happens if you declare an array without initialization? Well, it depends on the compiler which you use. Let's take a look at this simple example: int arr[5] = {}.

GCC

By default gcc does not produce any warnings/errors when you try to compile this code. Not even -Wall, but -Wpedantic does.

warning: ISO C forbids empty initializer braces

But anyway gcc fill members of an array with 0's exactly as if you specify it explicitly int arr[5] = {0} see assembly output godbolt.

CLANG

But default not showing warnings about this case, but with option -Wgnu-empty-initializer does:

warning: use of GNU empty initializer extension

Clang generates different assembly code godbolt but behaves the same.


No, an empty initializer list is not allowed. This can also be shown by GCC when compiling with -std=c99 -pedantic:

a.c:4: warning: ISO C forbids empty initializer braces

The reason is the way the grammar is defined in §6.7.9 of the 2011 ISO C Standard:

initializer:
         assignment-expression
         { initializer-list }
         { initializer-list , }
initializer-list:
         designation(opt) initializer
         initializer-list , designation(opt) initializer

According to that definition, an initializer-list must contain at least one initializer.