2

I have an error

initializer element is not constant

when I initialize variable in global scope this is my wrong code

char x = 65 ;
int c =  x ;
int main(void) {

    printf("%d",c); /* prints !!!Hello World!!! */
    return EXIT_SUCCESS;
}

but when i initialize int variable inside main functions it works correctly

char x = 65 ;
int main(void) {
    int c =  x ;
    printf("%d",c); /* prints !!!Hello World!!! */
    return EXIT_SUCCESS;
}

1 Answers1

2

Initializers for global variables must be a compile time constant. The value of another variable (even a const variable) is not a compile time constant.

A numeric constant (or an expression consisting only of numeric constants) is a compile time constant.

dbush
  • 205,898
  • 23
  • 218
  • 273