Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Compile time details of an 'extern variable'

Reply
Thread Tools

Compile time details of an 'extern variable'

 
 
bg
Guest
Posts: n/a
 
      02-28-2008
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.
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      02-28-2008
bg said:

<snip>

> 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.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      02-28-2008
bg wrote:
>
> 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.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
computation at compile time i.e. compile time functions usingtemplates Carter C++ 2 03-04-2009 06:43 PM
cant compile on linux system.cant compile on cant compile onlinux system. Nagaraj C++ 1 03-01-2007 11:18 AM
Is time.time() < time.time() always true? flamesrock Python 8 11-24-2006 06:51 AM
Show Details/Hide Details link button =?Utf-8?B?Sm9l?= ASP .Net 1 03-13-2006 04:51 PM
Warning "Re: Your details" & "Re: My details" Joe Lee Computer Support 3 05-17-2004 05:08 PM



Advertisments