Tuesday, November 10, 2015

Storage classes in C

Storage class defines the memory region that program variables an function going to use. Storage class also defines the scope of variables and function, how far and how long they can be used in program. They are mainly of four types
1. Auto or Local
2. Static
3. External
4. register

Automatic or Local 

local variables have very limited scope in program, its life time is only limited to function scope. as long as function in execution state variables retain their value and cleared as soon as it function comes out form execution. So it is very simple all variable declared inside function body is called local variable and their scope is within the function body, we can not access their value out side from the function. All local variables are stored in Stack.

Why auto or local variables can't retain their values because, stacks gets cleared after every execution and new set of function and variables will be loaded. So memory address given for a variable no longer available and no value is stored.

'auto' keyword used to define local variables, but it is optional.

Prototype
auto data-type variable-name  

Static

Statically defined variable and function has file scope. File scope means they can accessible within the file, out side of file it wont retain their values. It retains their value(last modified value) within the file.
Even is declared within function body, it retain its last modified value but its scope is limited to the function body itself.

But if it is declared globally it will be accessible by all the function used in that file.

As i mentioned in above statement Static is limited to file only so same variable name can be used in the other files also. but it will created confusion for programmer. so try to avoid.

Yes but if it is used with function then it will become very easy for programmer to use same function name but different bodies in different files.

statically defined variables are stored in BSS segment , if uninitialized and data segment, if initialized.

'static' keyword is used to define static storage class.

Prototype

static data-type variable-name  
or
static return-data-type function-name (arg1, arg2, ...);  

External 

External variables have global scope. they can be accessible my anywhere from the code or file it it is exported into that files. They can retain their values al long as program is running. External variables  are stored in BSS segment , if uninitialized and data segment, if initialized.

Functions used without storage class is External by default. To make function static  or local we have to use static or auto keyword explicitly.

 extern data-type variable-name  

Register 

Resister is equivalent to auto except that register variables are stored in register not in physical memory(RAM) and it is way out faster then auto. But size of registers are very small, depends on the system architecture. Whenever size of register gets full it will automatically store next variables to
stack without giving any prior notice.

'register' keyword is used to declare register variables.

register data-type variable-name  

No comments:

Post a Comment