Search code examples
clanguage-lawyerexterninternal-link

Simultaneously creating internal and external linkages in C


I was reading a C reference about linkage (external, internal and none) and came across the following:

If, within a translation unit, the same identifier appears with both internal and external linkage, the behavior is undefined.

I wanted to know how this undefined behavior can occur. Based on what I had read, a variable can have only one storage class. So it cannot be declared both static and extern at the same time.

So in what scenario can a variable have both internal and external linkage?


Solution

  • In this code:

    extern int x;
    static int x;
    

    The first declaration says x has external linkage, and the second declaration says it has internal linkage.