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

天大地大妈咪最大 提交于 2020-11-29 09:21:28

问题


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;

It is given that the first three are definition and last one is declaration ..how come ?


回答1:


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




回答2:


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!




回答3:


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.




回答4:


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.




回答5:


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!



来源:https://stackoverflow.com/questions/11507423/why-is-creating-a-variable-using-extern-a-declaration-and-not-a-definition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!