Saturday, January 7, 2012

Static in C

Discuss all the details regarding Static variables in C.

Static (and global) variables are initialized during the compile-time, so the initial values will simply be embeded in the executable file itself.

1.static is the default storage class for global variables. The two variables below (count and road) both have a static storage class.




     static int Count;
     int Road;

     main()
     {
       printf("%d\n", Count);
       printf("%d\n", Road);
     }
2.'static' can also be defined within a function. If this is done, the variable is initalised at compilation time and retains its value between calls. Because it is initialsed at compilation time, the initalistation value must be a constant.




     void Func(void)
     {
       static Count=1;
     }
3.There is one very important use for 'static'. Consider this bit of code.




     char *Func(void);

     main()
     {
       char *Text1;
       Text1 = Func();
     }

     char *Func(void)
     {
       char Text2[10]="martin";
       return(Text2);
     }
'Func' returns a pointer to the memory location where 'Text2' starts BUT Text2 has a storage class of auto and will disappear when we exit the function and could be overwritten by something else. The answer is to specify:




     static char Text[10]="martin";
The storage assigned to 'Text2' will remain reserved for the duration if the program.

4.The static storage class specifier can only be applied to the following names:
  • Objects
  • Functions
  • Class members
  • Anonymous unions
You cannot declare any of the following as static:
  • Type declarations
  • Function declarations within a block
  • Function parameters 
5.The keyword static is the major mechanism in C to enforce information hiding. C++ enforces information hiding through the namespace language feature and the access control of classes.

Note:
Static (becoming global in header files) variables can be defined in .h file. However, their definition should also be provided in the same header file. But , doing this makes the static variable as a private copy of the header file ie it cannot be used elsewhere. In normal cases, this is not intended from a header file.However you should make sure that the header is accepted only once by including #ifndef and #ifdef. By this the compiler will not raise errors saying redefinition of same variable.

#ifndef __static_var_definitions__
#define __static_var_definitions__

static int gblCounter;

#endif

2 comments:

  1. void countFunction(void)
    {
    static int var = 0;
    var = var + 1;
    printf("Value is %d\n", var);
    }
    The FIRST TIME YOU CALL the function var will be initialized as 0 and the function will show "Value is 1". Nevertheless, at the second time, var will be already allocated and at a global area. It will not be initialized again and the function will display "Value is 2".

    ReplyDelete
  2. Static variables and speed of execution---
    http://stackoverflow.com/questions/6636295/static-variables-in-functions

    static variables and Storage---
    http://stackoverflow.com/questions/4755541/static-variable-storage

    static extern vs extern static----
    http://stackoverflow.com/questions/7484773/static-extern-vs-extern-static

    Accessing static variable from another file---
    http://stackoverflow.com/questions/3989607/accessing-static-variable-value

    static variable to point next element---
    http://stackoverflow.com/questions/3697961/c-static-variable-problem

    ReplyDelete