Tuesday, February 7, 2012

What are STATIC variables ??

The storage class, static , has a lifetime lasting the entire program. static storage class can be specified for automatic (local) as well as global variables.

Static automatic variables continue to exist even after the block in which they are defined terminates. Thus , the value of a static variable in a function is retained between repeated function calls to the same function. 

Static variables are allocated on the heap. The scope of static automatic variables is identical to that of automatic (local) variables,i.e. it is local to the block in which it is defined; however the storage allocated becomes permanent for the duration of the program.

Static variables may be initialized in their declarartions ; however the initializers must be constant expressions , and initialization is done only once at compile time when memory is allocated for the static variable.

The scope of a static global variable is only within the file which it is declared. A user cannot use extern in a different file and access the static global variable.

No comments:

Post a Comment