Thread: static storage class

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305

    static storage class

    I had a doubt about the static keyword.

    [insert]
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    static int count;
    int road;
    
    void func();
    
    int main(void)
    {
    	static int i = 3;
    
    	printf("\n %d", i);
    	printf("\n %d", count);
    	printf("\n %d", road);
    
    	i = 1;
    	count = 1;
    	road = 1;
    
    	for(i = 0 ; i< 5 ; i++){
    		func();
    	}
    
    	printf("\n %d", i);
    	printf("\n %d", count);
    	printf("\n %d", road);
    
    	return 0;
    }
    
    void func(){
    	
    	static int i = 5;
    	i = 3;
    
    	printf("\n %d", i++);
    }
    i had a couple of questions regarding the usage of static keyword in c.

    1. Once i initialize the variable declared as static, it can be assigned a new value again. I thought that once a static variable is declared as static it cannot be changed to a new value. But then i tried the above program and the value of the variable declared as static can be changed. Is this true?

    2. Also in the code above main() i have declared two variables count, and road with road not having the static identifier. So is road a static variable or an auto variable.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    1. You can change the value of a static variable, just like any other variable.

    2. A global variable that is declared as static means that the variable is only accessible inside the source file it was declared in. A non-static global variable can be accessed from other source files that are linked together.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Okay but in this case since i have not appended any identifier to road so is it a static variable in this case.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by roaan View Post
    Okay but in this case since i have not appended any identifier to road so is it a static variable in this case.
    No, it's just a normal global variable.
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    First off, you're confusing const and static. const means read-only, which is what you appear to have assumed static meant.

    The static keyword in C is overloaded, with two common meanings (C99 added a third meaning!) When you use static with a file-scope (that is, a global) variable, it's entirely different than when you use static inside of a block.

    Now, in order to fully understand static, you have to understand the idea of storage duration. For the purposes of understanding the static keyword, you can consider two types of storage duration: static and automatic.

    Storage duration refers to the lifetime of the object; that is, when it is created and when it is destroyed. Objects with static storage duration are created when the program starts and destroyed when it ends (that is, they are alive as long as the program is running). Objects with automatic storage duration are created when their block is entered, and destroyed when it is exited. Often this means that they are created when a function begins, and are destroyed when the function is returned from.

    Global variables always have static storage duration. A global variable exists for as long as the program is running. Thus there is never any need to tell the compiler to give it static storage duration. Presumably in an effort not to create new keywords, the static keyword was thus given a meaning unrelated to storage duration when used with global variables. As bithub mentioned, global static objects cannot be seen from other source files. This facilitates data-hiding, a crude sort of version of an OO language's concept of “private”. Without the static keyword, global objects can be accessed from other source files. This is sometimes wanted, sometimes unwanted.

    Local variables (those declared in a block), by default, have automatic storage duration. However, it is sometimes useful to have a local variable retain its value even after the function exits. You can do this by tagging the object as static. In this case, static does give static storage duration to the object. You can, if you like, think of local static objects as global variables that are only accessible from that particular function.

    As an example of using local static variables, I have some code somewhere that adds a node to the end of a linked list. Thus I have a tail pointer so I can find the end without iterating each time; however, I have no other use for the tail pointer. Thus I created a static local tail pointer inside my function that adds a new node. The tail pointer keeps pointing to the end of the list even after the function returns, but no other functions can accidentally mess with it, because only a single function has access to it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Use of 'static' in a class
    By h3ro in forum C++ Programming
    Replies: 3
    Last Post: 01-21-2009, 11:48 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  4. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM