Why is creating a variable using 'extern' a declaration and not a definition?

后端 未结 5 1240
-上瘾入骨i
-上瘾入骨i 2021-01-21 22:42

I came across the following problem while reading ...just cant get the logic behind this.

auto int c;
static int c;
register int c;
extern int c;
5条回答
  •  借酒劲吻你
    2021-01-21 23:01

    The last one with extern does not define storage for c. It merely indicates that c exists somewhere and the linker should be able to resolve it to some global c defined elsewhere.

    If you compiled and linked a single .c file and tried to use the last c you'd have a linker error. With the the first 3 cs you would not as they have substance (they've been defined) in the current compilation unit.

    If you'd like to learn more about extern and declaration vs definition here's a good article on the topic. To quote from that article:

    Declaration of a variable/function simply declares that the variable/function exists somewhere in the program but the memory is not allocated for them

提交回复
热议问题