What are the various storage classes in C and their differences?
Storage classes in C are:
- auto
- extern
- static
- register
auto:
- auto is the default storage class for variables. If no storage class is mentioned for a variable, then it is auto
- if the variable is not initialized, garbage shall be assigned
- can be accessed only within the block they are declared
- the life of the auto variable is till the end of the block in which it is declared
extern:
- used for global variables
- when extern is used before global variables, the variable can be used outside the file in which it is declared
- default value assigned is zero
- life of the extern is till the end of the particular program in which it is declared
static:
- A local variable has scope till the end of it's block/function. Once the block is exited, the local variable is more valid and it's value is lost. If there is a need for a particular local variable to hold it's value between function calls, then static need to be used
- default initial value is zero
- life of the static variable is till the end of the program
register:
- once declared with this type of keyword, then the variable is stored in registers
- major advantage is that registers have faster access
- register variable has the same property as auto variable, this meant
- default garbage value
- scope within a block
- life of variable only till end of block in which it is declared
0 Comments