Compile time details of an 'extern variable'

B

bg

Hi All,

I'm bit confused of variables with global scope which is declared with
extern storage class.

Consider the following piece of code code in file A.c.


File: A.c
==================
#include "A.h"

int GData:

main()
{
GData = 5;
}

=====================
where file A.h is

File: A.h
==================
extern int GData;
==================


So effectively in A.c, the same variable is first declared as extern
and then defined in the same file.
Can anyone please tell me how will the c compiler interpret this ???

Also, whether there is any difference in the storage areas of global
variables, file scope static variables and function scope static
variables.
 
R

Richard Heathfield

bg said:

So effectively in A.c, the same variable is first declared as extern
and then defined in the same file.

Right. A ***declaration*** is precisely that - an announcement, by you, for
the compiler's benefit: "I DECLARE that such-and-such an object has some
storage reserved for it somewhere or other, perhaps even in a source file
that you can't see right now." You could lie to the compiler, and it would
believe you. (The linker wouldn't, though.)

A ***definition*** is an instruction to reserve storage for an object.

All definitions are declarations (because the act of reserving storage is a
great way to say "hey, this object has some storage!"), but declarations
need not also be definitions.

In your example, GData is declared in A.h, and defined in A.c. The fact
that A.c includes A.h doesn't matter - you can declare something and then
define it without confusing the compiler. (Indeed, it is sometimes
necessary to declare that something exists before defining it - consider,
for example, mutual recursion.)

The point of all this is that you can include A.h in, say, B.c, which does
*not* define GData, and yet use GData within B.c's code.

The fact that you /can/ doesn't mean that you /should/. Excessive use of
file scope data can cause maintenance headaches. (Trust me on this!)
Also, whether there is any difference in the storage areas of global
variables, file scope static variables and function scope static
variables.

The precise details of storage are up to the implementation, but all the
examples you give are examples of objects with static storage duration.
They are allocated storage at the beginning of the program (or at least,
the program behaves "as if" they were allocated at startup), and retain
their last-stored values throughout the program.
 
C

CBFalconer

bg said:
I'm bit confused of variables with global scope which is declared
with extern storage class. Consider the following piece of code
code in file A.c.

File: A.c
==================
#include "A.h"
int GData:

main() {
GData = 5;
}

=====================
where file A.h is

File: A.h
==================
extern int GData;

So effectively in A.c, the same variable is first declared as
extern and then defined in the same file. Can anyone please
tell me how will the c compiler interpret this ???

That's fine. The "extern int GData;" line allows any other .c file
that includes A.h to reference the GData variable.
Also, whether there is any difference in the storage areas of
global variables, file scope static variables and function
scope static variables.

Normally none. The 'static' alters the visibility of the names.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top