Friday, April 13, 2012

Static storage class in C

Static  storage class will be used when the value of the variable should be persistent between the function calls. default value for the static variable is zero. And scope of the variable or function is limited to the file only.

  • Keyword: static
  • Default value: zero if not initialised .
  • Memory Location: Main memory
  • Scope:  Local to the function or block
  • Lifetime:  till the program or application completes
Sample code for static storage class:
int test()
{
    int a=10; // local to test() function
    printf("a value is %d\n",a);
    a++;
}
int static_test()
{
    static  int st=10; // stativ variable, value will be restored
    printf("st value is %d\n",st);
    st++;
}
main()
{
    printf("************************** non-static value **********************************\n");
    test();
    test();
    test();
    printf("************************** static value **********************************\n");
    static_test();
    static_test();
    static_test();

}

Output:
**************** non-static value **********************************
a value is 10
a value is 10
a value is 10
************************** static value **********************************
st value is 10
st value is 11
st value is 12

From the above sample code, it is clear that static variable in static_test()  function is restoring the value by incrementing one, where as in test() function value is always same.


Static Initialization:  Static variable initialises only one time in whole program. And it restores the new value when changes. See the sample code below.

main()
{
    for(int j=0;j<5;j++)
    {
        static int i=10; // this executes only once, it wont execute for each iteration
        printf("i is %d\n",i++);
    }
}

Output:
i is 10
i is 11
i is 12
i is 13
i is 14

In the above sample code, static integer variable initialised in for loop. Because of static property, it wont initialise for each iteration. for auto variables it will initialise for each iteration.

Scope Limitation: Scope of the variable is limited to the file if you declare it as static. see the sample code below.
// file name is file1.c and code is below

static int i=10; // i is static, so its scope is limited to this file only
main()
{
    print();
}

// file name is file2.c  and code is below

#include 
extern int i; // i is extern, so i value is somewhere in ohter file 
extern void print();
void print()
{
    printf("i is %d\n",i); // will get error here, because in file1.c i is static
}

Output with static:
/tmp/ccwXbAno.o: In function `print':
file2.c:(.text+0x6): undefined reference to `i'

Output without static:
i is 10

 In the above code, there are two files file1.c and file2.c. static variable i is declared and initialised in file1.c, and trying to access in file2,c in print function. at this time we will get the error, because of declaring i in file1.c as static.  if the variable is not static we will get the output shown above.

P.S:  Static keyword behaviour varies in C++. Click here for static behaviour in C++.

No comments:

Popular Posts