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

后端 未结 5 1242
-上瘾入骨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 22:53

    The first 3 statements actually allocate a place for the int.

    The last one does not. All it does it tell the compiler is that somewhere in another compilation unit, an int called c will be defined.

    If it is not defined, you'll get a linker error later on. Unsurprisingly enough, the linker will say that c is not defined.

    0 讨论(0)
  • 2021-01-21 22:54

    First three are definition because it will set aside storage for the variables.

    Last one will not allocated any storage for int c. It will just use storage allocated and named elsewhere.

    0 讨论(0)
  • 2021-01-21 22:56

    The keyword extern references the fact that the definition of the variable (or possibly function) is elsewhere; the compiler then links this declaration to a defined body in a separate file. The prior three keywords state a declaration - the variable is not defined elsewhere and therefore are not prototypes.

    For instance, say you have a project structure like so:

    ..
    -- main.c
    -- client.c
    -- client.h
    -- server.c
    -- server.h
    

    When gcc compiles these using the header files, the header files typically will define the variables required for the program. This allocates a symbol that links to the declaration of the symbol in the .c files. This is how compilers link up various project files with .o objects. You may be further interested in how this all appears by using objdump -d (assuming you're on Linux) to debug the actual disassembled structure of your program.

    Enjoy and good luck!

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-01-21 23:05

    The four sentences are declarations, however the first three sentences are also definitions.

    Read here about the difference between declaration and definition.

    auto, static and register are modifiers for the variable. Read de documentation about them.

    extern is only declaration because you are telling the compiler that the definition of the variable or function is somewhere else - in another C module -.

    Hope it helps!

    0 讨论(0)
提交回复
热议问题