Search code examples
cstandardsconstantfolding

Is compile-time constant expression evaluation ever required by the C standard?


For example, is

static int a[1+1];

valid standard C? For some or all versions of the standard?

I'm not interested in whether compilers can handle it, but whether it is part of standard C.


Solution

  • C11, §6.6:

    A constant expression can be evaluated during translation rather than runtime, and accordingly may be used in any place that a constant may be.

    So yes, simple constant folding is mandatory and this snippet is valid standard C.