• Storage class helps us to define
1. Where to store the variable
2. What is the initial value of variable?
3. How much is the life of a variable?
4. What is the scope of the variable?
• There are 4 storage classes – auto, static, register, and extern.
• auto:
o An auto variable is created each time the block in which it is declared is entered and is destroyed each time the block is exited.
o Such variable can be declared only in functions.
o By default, a local variable is auto but it can be declared to be of another storage class by the use of explicit storage class in its declaration.
o A function cannot be declared as auto.
• static
o These variables remain in existence throughout the execution of the program.
o Global and local variables can be declared as static but not formal arguments.
o When a global variable or function is declared explicitly to be static, its scope is restricted to the file in which it is declared.
• register
o A local variable or formal parameter can be declared to be register.
o Here declaration means to store the variable in a machine register instead of the main memory.
o It is like auto variable since it disappears when the function exists.
o The only restriction is that a register variable must be of a type whose size is not longer than the size of a register.
o The address of register cannot be taken and hence & operator may not be used.
o A function of global variable cannot be declared as register.
• extern
o This storage class does not allocate any memory for a variable but declares it to have been created elsewhere in the program.
o This declaration must be used to access a global variable declared in another file in which it may be used in either global or local declaration.
o A formal parameter cannot be extern.
o A function is by default extern although this storage class needs never to be used in a function definition declaration.
1. Where to store the variable
2. What is the initial value of variable?
3. How much is the life of a variable?
4. What is the scope of the variable?
• There are 4 storage classes – auto, static, register, and extern.
• auto:
o An auto variable is created each time the block in which it is declared is entered and is destroyed each time the block is exited.
o Such variable can be declared only in functions.
o By default, a local variable is auto but it can be declared to be of another storage class by the use of explicit storage class in its declaration.
o A function cannot be declared as auto.
• static
o These variables remain in existence throughout the execution of the program.
o Global and local variables can be declared as static but not formal arguments.
o When a global variable or function is declared explicitly to be static, its scope is restricted to the file in which it is declared.
• register
o A local variable or formal parameter can be declared to be register.
o Here declaration means to store the variable in a machine register instead of the main memory.
o It is like auto variable since it disappears when the function exists.
o The only restriction is that a register variable must be of a type whose size is not longer than the size of a register.
o The address of register cannot be taken and hence & operator may not be used.
o A function of global variable cannot be declared as register.
• extern
o This storage class does not allocate any memory for a variable but declares it to have been created elsewhere in the program.
o This declaration must be used to access a global variable declared in another file in which it may be used in either global or local declaration.
o A formal parameter cannot be extern.
o A function is by default extern although this storage class needs never to be used in a function definition declaration.
No comments:
Post a Comment