Thread: static global variable and static extern global variable

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    17

    static global variable and static extern global variable

    Hello all,


    What is the difference between static global variable and static extern global variable in c ?

    PLese Help me..!!


    Thanks,
    Gunjan

  2. #2
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    "static" can be used to limit the scope of global variable to only the file it is declared in. I don't know what "static extern" does, the two seem contradictory. Intuition says it should cause an error.

  3. #3
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Static and extern don't go together. You have to choose one.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    What about the use of 'static' inside a function. The variables thus declared maintain their values across function calls. So in that sense, they are "global", as long as the function itself is callable from any module.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by nonoob View Post
    What about the use of 'static' inside a function. The variables thus declared maintain their values across function calls. So in that sense, they are "global", as long as the function itself is callable from any module.
    I think "global" is not quite what you're looking for. Their lifespan is the entire run-time, not just the duration of the function call. True, they are usually located in the data or BSS section like global variables, but they don't have global scope, and thus can't be made available to other translation units (.c files), even with extern. Their name is only valid within that function.

    EDIT: according to section 6.2.2, paragraph 7 of the standard: "If, within a translation unit, the same identifier appears with both internal and external
    linkage, the behavior is undefined."
    Last edited by anduril462; 01-11-2011 at 10:51 AM.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Correct. Sorry... I confused scope (direct syntactical accessibility) with ultimate accessibility (can get to the data in some way from any other translation unit). I was just thinking of BSS from a compiler's point of view. My use of the word 'global' was somewhat loose.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    17
    Thanks to all..

    But i read over internet that "Static storage class can be specified for automatic as well as external variables such as:
    static extern varx; "

    Still i m confuse what is the purpose of using two storage class at a time as static limit the scope of global variable.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    There is no purpose...

    Unless you read that in the standard (which you didn't), it doesn't really matter what it says "on the internet". That person doesn't know what they're talking about. There are two distinct concepts here, linkage and storage duration. I blame (at least in part) the creators of the C language for this common misunderstanding, because they reused the static keyword for two completely different purposes. But such is life...so here's the difference:

    Linkage basically describes where you can see the variable/function. Using 'static' for global variables and functions means they're only visible to that translation unit (synonym for a .c file). The other option is 'extern', which is a way of describing the type of some variable/function, but stating that you will find the details (function body or storage space for said variable) in another .c file. I hope this makes things like "static extern ..." seem ridiculous. The standard says this behavior is "undefinied", which means anything at all, or nothing, may happen. Don't trust code like this.

    Storage duration describes when there is valid space to put stuff in said variable. By default, variables local to a function have automatic storage, meaning that space is allocated when entering the function and deallocated when the function exits. Global variables, on the other hand, have space allocated throughout the duration of the program. When using the 'static' keyword with a local variable, you are stating that it's lifespan is the duration of the program. There are other complexities that go along with this, primarily that all instances of a function share a single instances of that variable, which gives problems with recursive or re-entrant functions.

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    17
    Thanks a lot..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scope of global variable vs. static
    By chiefmonkey in forum C++ Programming
    Replies: 4
    Last Post: 06-21-2009, 12:23 PM
  2. Static Local Variable vs. Global Variable
    By arpsmack in forum C Programming
    Replies: 7
    Last Post: 08-21-2008, 03:35 AM
  3. global and static variable in a class delivered in a DLL
    By George2 in forum C++ Programming
    Replies: 16
    Last Post: 04-13-2008, 08:19 AM
  4. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  5. Static global variable acting as global variable?
    By Visu in forum C Programming
    Replies: 2
    Last Post: 07-20-2004, 08:46 AM