Search code examples
cgccglobal-variablesconstantsextern

Why does "const extern" give an error?


The following piece of code working fine:

#include <stdio.h>

extern int foo; // Without constant
int foo = 42;

int main() 
{
    printf("%d\n",foo);
    return 0;
}

But, the following piece of code give an error:

#include <stdio.h>

const extern int foo; // With constant
int foo = 42;

int main() 
{
    printf("%d\n",foo);
    return 0;
}

So, why does const extern give an error?


Solution

  • Standard says:
    C11-§6.7/4

    All declarations in the same scope that refer to the same object or function shall specify compatible types

    const int and int are not compatible for the same object foo in the same scope.