3

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 ?

4
  • Why the downvotes? it's a valid question for this forum. Commented Jul 16, 2012 at 15:32
  • 2
    All four are declarations. The first three are also definitions. Commented Jul 16, 2012 at 15:35
  • @Blank - It is a silly question. extern means defined somewhere else. Why is that not a definition? Commented Jul 16, 2012 at 17:28
  • 3
    Silly just means you know the answer... Commented Jul 16, 2012 at 18:01

5 Answers 5

4

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

Sign up to request clarification or add additional context in comments.

Comments

3

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!

Comments

2

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.

Comments

2

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.

Comments

2

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.