3

In the following code a array is initialized with s "seed" variable, which is clearly not constant expression (as it's evaluated "run-time"):

#include <stdio.h>

int main(void)
{
    int s = 1, i;
    int a[] = {s, s + 2, s + 4, s + 6, s + 8};

    for (i = 0; i < (int) (sizeof a / sizeof a[0]); i++)
        printf("%d ", a[i]); /* prints: 1 3 5 7 9  */
    putchar('\n');

    return 0;
}

It compiles in gcc -Wall -Wextra with no warnings. However adding -pedantic provokes:

check.c: In function ‘main’:
check.c:8: warning: initializer element is not computable at load time
check.c:8: warning: initializer element is not computable at load time
check.c:8: warning: initializer element is not computable at load time
check.c:8: warning: initializer element is not computable at load time
check.c:8: warning: initializer element is not computable at load time

Does C require constant expression for initializer elements ?

Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
  • 1
    no, since C99. try add `-std=c99` – BLUEPIXY Jun 21 '14 at 12:09
  • 2
    Duplicate of [Error: initializer element is not computable at load time](http://stackoverflow.com/questions/160960/error-initializer-element-is-not-computable-at-load-time) – WhozCraig Jun 21 '14 at 12:11
  • @WhozCraig: Thanks, I didn't notice it's already answered, maybe I should remove this question as not useful. – Grzegorz Szpetkowski Jun 21 '14 at 12:16
  • 1
    @GrzegorzSzpetkowski not really. The posted answer is additionally helpful due to the standard citation. I'd keep it and mark it done if it helped. – WhozCraig Jun 21 '14 at 12:19

2 Answers2

7

This is valid in c99 but not valid in c89 (emphasize mine):

(C89, 6.5.7) "All the expression in an initializer for an object that has static storage duration or in an initializer list for an object that has aggregate or union type shall be constant expressions"

but

(C99, 6.7.8p4) "All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals."

By default, gcc compiles with -std=gnu89 which is c89 + GNU extensions.

ouah
  • 142,963
  • 15
  • 272
  • 331
  • Is `s+2` a constant expression in C99? – Bill Lynch Jun 21 '14 at 12:13
  • 2
    +1 stressing the **static storage duration**. Const expressions are required for globals, local `static`, etc.; automatics get a pass in C99. – WhozCraig Jun 21 '14 at 12:14
  • @sharth `s + 2` is not a constant expression but an array initializer has to be a constant expression in c89. In c99 if the array is not static, the initializers are not required to be constant expressions. – ouah Jun 21 '14 at 12:17
  • @ouah: Yeah. I thought `s+2` wasn't a constant expression, but I missed the bit about static. Thanks for clarifying. – Bill Lynch Jun 21 '14 at 12:18
2

In addition to ouah's excellent answer I would add that C99 requires constant expression for designators within designated initializers (only in C99), e.g. following array initialization is invalid in C99:

int s = 1, i = 0;
int a[] = {[i] = s, [i+1] = s + 2, [i+2] = s + 4, s + 6, s + 8};

which might be rewritten as e.g.:

#define I 0

int s = 1, i;
int a[] = {[I] = s, [I+1] = s + 2, [I+2] = s + 4, s + 6, s + 8};
Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137