Compile time details of an 'extern variable'

Discussion in 'C Programming' started by bg, Feb 28, 2008.

  1. bg

    bg Guest

    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.
     
    bg, Feb 28, 2008
    #1
    1. Advertising

  2. 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
     
    Richard Heathfield, Feb 28, 2008
    #2
    1. Advertising

  3. bg

    CBFalconer Guest

    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
     
    CBFalconer, Feb 28, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Sm9l?=

    Show Details/Hide Details link button

    =?Utf-8?B?Sm9l?=, Mar 13, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    927
    dkode
    Mar 13, 2006
  2. Thomas Matthews
    Replies:
    5
    Views:
    2,485
    tom_usenet
    Aug 2, 2004
  3. Nagaraj
    Replies:
    1
    Views:
    889
    Lionel B
    Mar 1, 2007
  4. Carter
    Replies:
    2
    Views:
    518
    Carter
    Mar 4, 2009
  5. Andre
    Replies:
    5
    Views:
    550
    Keith Thompson
    Jul 17, 2012
Loading...

Share This Page