C - initializer not constant with string literal

I know the limitations for initializers in the file area in c: you cannot use a variable (even const), functions, etc. But I really don't understand why this is not working:

#include <stdlib.h>
#include <stdio.h>

//unsigned long a = "string"[0] > '0'; // this does not compile
unsigned long a = 's' > '0'; // this works fine, output is "a = 1"

int main(void)
{
    printf("a = %lu\n",a);

    return 0;
}

Why does a string literal string give: error: the initializer element is not a constant. String literals not considered constant? Is there any way to make it work?

Thank you in advance

+6
source share
2 answers

Your variable has a static storage duration as such and according to N1570 (C11) §6.7.9 / p4 :

, .

§6.4.5/p6 ( ). , C . §6.6/p9, :

- , lvalue ; & , , . The - [] -. → , & * , - , .

, , .

+6

C , .

"string"[0] ? ?

6.6p2

, , , , , .

: , , [] . , . .

+2

All Articles