Tuesday, January 5, 2010

C Storage classes

Storage classes specifies where the variable need to be created, life time of the variable and scope of the variable.

There are four storage classes supported in C programming language.
1. Auto.
2. Register.
3. Static.
4. Extern.

When discussing about the storage classes, we mainly need to consider the life of the variable, scope of the variable(means is it possible to access this variable this at this point of time?) and storage location of the variable.

1. Auto specifier is the default storage class specifier for variables declared inside a function.
This variable life remains will be with in the scope of the function. i.e, when function scope } ended, this variable will go out of the memory and will not be accessible.

2. Register specifier also similar to auto variable but the storage location is CPU Registers instead of RAM. So the variables can be accessed by the processor quickly compared to the variables stored in RAM. Also it is not guaranteed that register keyword, will store the variable in CPU registers.

"What is the CPU register name which stores the variable when register keyword is used to declare the variable?"

3. Static storage specifier, will create the global variable(i.e life time is till end of the program) which can be accessed only with in the scope of the declaration.
So, when static variable is declared inside a function, then it can be accessed only with in the function, and its value initialized during the first call of the function. Value retained till end of the program. Example usage: 1. Static counters. 2. Avoid localization issues(Hide the variable to outside the scope)

4. Extern storage specifier, indicates that variable might be defined in some other source file and reference of that global variable is declared and used in some other source file.

What is the difference between static and global variables?
Answer1
Variables defined local to a function disappear at the end of the function scope. So when we call the function again, storage for variables is created and
values are reinitialized. So if we want the value to be extent throughout the life of a program, we can define the local variable as "static." Initialization is performed only at the first call and data is retained between func calls.

Had it been gloal variable, it would have been available outside the scope of the function, but static variable is not available outside the scope of a function (helpful in localizing errors - as it can't be changed outside the func
scope).

Answer2
Static and global variable differ a lot in their behaviour to life and scope. First, let me distinguish between life and scope. Life of an object determines whether the object is still in the memory (of the process) whereas scope of the object is whether can I know the variable by its name at this position. It is possible that object is live, but not visible (not in scope) but not that object is not alive but in scope (except for dynamically allocated objects where you refer object through pointers).

Static variables are local in scope to their module in which they are defined, but life is throughout the program. Say for a static variable inside a function cannot be called from outside the function (because it's not in scope) but is alive and exists in memory. The next time this function is entered (within the same program) the same chunk of memory would be accessed now retaining the variables old value and no new memory is allocated this time for this variable like other variables in the function (automatic variables). So basically the variable persists throughout the program. Similarly if a static variable is defined in a global space (say at beginning of file) then this variable will be
accessible only in this file (file scope).

No comments:

Post a Comment