Default Storage Class for Global Variables

This is a discussion on Default Storage Class for Global Variables within the C Programming forums, part of the General Programming Boards category; Hey all, I am trying to further understand the use of the keyword "static" for global variables. I know that ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    2

    Default Storage Class for Global Variables

    Hey all,

    I am trying to further understand the use of the keyword "static" for global variables. I know that if I declare a global variable as static, it will only be "visible" within the c file that it was declared in. I also understand that if I try to "extern" a variable declared as static in another file, it should not work.

    Code:
    //file1.c
    
    extern int x; //shouldn't work
    //extern static int x; //also shouldn't/wouldn't work
    extern int y; //does work fine;
    
    int main()
    {
    	return 0;
    }
    Code:
    //file2.c
    
    static int x;
    int y;
    Here is what I don't understand: I have read on more than one website, that the default storage class for global variables is "static". If that is the case, why does "extern int y;" work in my example above? Granted, I want it to work the way it does, but if the default storage class IS static, how can it? I'm probably thinking about this too hard...

    NOTE: I am compiling on Ubuntu Linux using gcc. Also note, I am only refering to c, not c++.

    Thanks for any clarification you can provide!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > I have read on more than one website, that the default storage class for global variables is "static".
    There are a lot of websites which are wrong then.

    Global variables (and functions for that matter) are extern unless you put static in front of them.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    2
    Thank you very much for your quick reply. I suppose this is a prime example of why we must watch what we read!

    Thanks again!

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,708
    What you are probably reading is that global variables have static duration, with external linkage. See: Static (C++)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class Public Variables not holding their value
    By carrotcake1029 in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2009, 04:59 PM
  2. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  3. Replies: 3
    Last Post: 10-31-2005, 11:05 AM
  4. Global variables used in a linked class, getting errors.
    By RealityFusion in forum C++ Programming
    Replies: 3
    Last Post: 09-24-2005, 12:25 AM
  5. Class member variables only readable by member functions?
    By _Elixia_ in forum C++ Programming
    Replies: 4
    Last Post: 10-10-2003, 03:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21