Thread: global vs static variable

  1. #1
    Registered User
    Join Date
    Jul 2018
    Posts
    81

    global vs static variable

    I see a difference between global and static variables These two variables do the same thing. But the theory says that these two are different
    Code:
    #include <stdio.h>
    
    static int i = 0;
     
    
    
    void m() {
    static int j = 0;
          j++;
    	  printf("%d \n", j);
    }
    
    
    int main() {
        m();
        m();
    	
    	i++; 
    	printf("%d \n", i);
    	i++;
    	printf("%d \n", i);
      
        return 0;
    }
    1
    2
    1
    2

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    i is not a "true" global variable: because of the use of the static keyword, it is a file scope variable that is restricted to the current translation unit (i.e., source file + headers), i.e., another file scope variable named i in a different source file would refer to a different object. We say that the static keyword causes the identifier i to have internal linkage.

    If you omitted the static keyword, then i would have external linkage, i.e., unless it is shadowed by another variable named i in a local scope, all variables named i would refer to this object.

    With or without the static keyword, the object referred to by i has static storage duration, i.e., its lifetime is that of the program.

    j, on the other hand, is a static local variable. Like i, it has static storage duration. Unlike i, it is local, i.e., the only place where j refers to that particular object is in the scope of m().
    Last edited by laserlight; 02-04-2020 at 07:13 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by laserlight View Post
    i is not a "true" global variable: because of the use of the static keyword, it is a file scope variable that is restricted to the current translation unit (i.e., source file + headers), i.e., another file scope variable named i in a different source file would refer to a different object. We say that the static keyword causes the identifier i to have internal linkage..
    A little hard to understand

    What is the similarity and difference between global and static variable ?

    similarity
    Both will variables will initialize once they will not create and destroy each time

    Both variables will alive as long as the program is running

    difference
    We always declare the global variable outside the main function, whereas the static variable can be local or global.

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Depending on the compiler (usually older ones), global variables may not be initialized, unless explicitly initialized, while static variables are initialized to zero by default.

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    Quote Originally Posted by rcgldr View Post
    Depending on the compiler (usually older ones), global variables may not be initialized, unless explicitly initialized, while static variables are initialized to zero by default.
    A compiler that doesn't initialize global variables is non-standard and should be used with care (or, if possible, discarded in favor of one that does follow the relevant standard(s)).

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by christop View Post
    A compiler that doesn't initialize global variables is non-standard and should be used with care (or, if possible, discarded in favor of one that does follow the relevant standard(s)).
    When and where is it stated that global variables are initialized to zero if there is not a initial value used?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,791
    Quote Originally Posted by stahta01 View Post
    When and where is it stated that global variables are initialized to zero if there is not a initial value used?

    Tim S.
    Since ANSI/c89

    6.7.9p10

    If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static or thread storage duration is not initialized explicitly, then:
    • if it has pointer type, it is initialized to a null pointer;
    • if it has arithmetic type, it is initialized to (positive or unsigned) zero;
    • if it is an aggregate, every member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;
    • if it is a union, the first named member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;

    Edit:

    Just realised that 6.2.4p3 is also relevant
    An object whose identifier is declared without the storage-class specifier _Thread_local, and either with external or internal linkage or with the storage-class specifier static, has static storage duration. Its lifetime is the entire execution of the program and its stored value is initialized only once, prior to program startup.
    Last edited by Hodor; 02-05-2020 at 11:36 PM.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Hodor View Post
    Since ANSI/c89

    6.7.9p10



    Edit:

    Just realised that 6.2.4p3 is also relevant
    So, it says nothing about global variables!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    It ultimately stems from the fact that in a multi-user machine, memory initially allocated to a new process is zeroed by the OS, lest it contain information from a previous process, possibly run by a different user.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  10. #10
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,791
    Quote Originally Posted by stahta01 View Post
    So, it says nothing about global variables!

    Tim S.
    Yeah it does. 6.2.4p3

    "global" variables are initialised as if they were declared static

    Edit: It's talking about storage duration here, not linkage. "globals" in C have static storage duration and are therefore initialised as per 6.7.9
    Last edited by Hodor; 02-05-2020 at 11:45 PM.

  11. #11
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by stahta01 View Post
    When and where is it stated that global variables are initialized to zero if there is not a initial value used?
    Quote Originally Posted by Hodor View Post
    Since ANSI/c89
    By older compilers, I meant ones that aren't c89 compliant. This would include most 16 bit compilers for X86 (like Microsoft (although the last 16 bit tool set was released in 1994, I don't think it's c89 compliant)). You would need to be running on a 16 bit or 32 bit OS, perhaps using a virtual machine, in order to run these old compilers and 16 bit programs. Rarer still would be compilers for the old 68000 systems (Amiga, Atari ST, Macintosh).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static global variable and static extern global variable
    By gunjansethi in forum C Programming
    Replies: 8
    Last Post: 01-12-2011, 01:00 AM
  2. Replies: 8
    Last Post: 09-27-2010, 04:11 PM
  3. Static Local Variable vs. Global Variable
    By arpsmack in forum C Programming
    Replies: 7
    Last Post: 08-21-2008, 03:35 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

Tags for this Thread