Thread: Need help: Explanation of a static storage class

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    3

    Need help: Explanation of a static storage class

    I am going through a tutorial on C and just ran into static storage class on an int. Reading this code I would assume that the variable number is set to 7, print statement says number is 7. Than 7 turns to 8, we return to the top of the loop, "number is than set to 7 again", than we print number is 7. Can someone help me understand how 7 gets incremented and than holds that value even though when it loops through again number is defined again and set to 7.

    Code:
    int main( int argc, char ** argv )
    Code:
    {
        for (int i =1; i <= 5; ++i) {
            static int number = 7;
            printf("number is %d\n" , number++);
        }
        return 0;
    }


    Output:

    number is 7
    number is 8
    number is 9
    number is 10
    number is 11

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Example
    Code:
    void foo(void)
    {
         static int var = 7;/* This runs only once*/
         printf("%d\n", var);
         var++;
    }
    
    int main(void)
    {
         // call foo 4 times
         foo();
         foo();
         foo();
         foo();
     
         return 0;
    }
    The output would be:
    7
    8
    9
    10
    Why?
    As you may know, local variables terminate their lifetime, when the function they live in terminates.
    var, in our example, is a static variable though. As a result, it is "immortal". It won't die, no matter how many times the function the terminates. It is created once and dies once, when your program ends.
    As a result, the value of the variable is remembered among function calls!
    But, this line of code
    Code:
    static int var = 7;
    runs only once, because the static variable is created one time only!


    Notice, that static keyword can also been found for limiting the scope of a variable, making her have in -file scope. However, the static keyword has not the same effects on these situations.

    //Also, Sam, welcome to the forum
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    static variables are initialized to a value ONLY once, but they are not constant, and can be changed. Between calls to a function, their value will remain in place.

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    3
    Thank you for the explanation, and the welcome. That makes perfect since now. "runs only once, because the static variable is created one time only!"
    I tested this out using and not using static, this was actually blowing my mind for a second. I appreciate the help.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by Sam Pertuit View Post

    Code:
    int main( int argc, char ** argv )
    Code:
    {
        for (int i =1; i <= 5; ++i) {
            static int number = 7;
            printf("number is %d\n" , number++);
        }
        return 0;
    }



    The easiest way to think of it is that the line which says "static int number = 7" is executed exactly once, at compile time. Therefore, each time you enter the block where number is defined, it retains its previous value.

    Note that there is also "static linkage" which does something different. For items with static linkage it also makes sense to define a static const member which cannot be changed.



  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Sam Pertuit View Post
    Thank you for the explanation, and the welcome. That makes perfect since now. "runs only once, because the static variable is created one time only!"
    I tested this out using and not using static, this was actually blowing my mind for a second. I appreciate the help.
    You are welcome
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    3
    Thanks for the heads up on the "static linkage", I will be looking for this shortly.

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Sam Pertuit View Post
    Thanks for the heads up on the "static linkage", I will be looking for this shortly.
    I hope you read my last sentence on post #2, which is about linkage.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doubt regarding storage of a static variable
    By karthik537 in forum C Programming
    Replies: 6
    Last Post: 02-21-2012, 01:21 PM
  2. Static storage duration question.
    By nimitzhunter in forum C Programming
    Replies: 5
    Last Post: 08-10-2010, 01:18 PM
  3. static storage class
    By roaan in forum C Programming
    Replies: 4
    Last Post: 09-08-2009, 04:57 PM
  4. Dynamic storage class?
    By Xinok in forum C++ Programming
    Replies: 12
    Last Post: 12-20-2005, 01:14 PM
  5. Storage Class
    By Blanket in forum C++ Programming
    Replies: 5
    Last Post: 04-15-2003, 09:54 PM