07 Scope and Storage Classess
07 Scope and Storage Classess
of Variables
Global, Local, Extern, Auto, Static, Register
Scope of Variable
Scope of variable specifies the visibility of variable such that a declared
variable can be used in which part of the program.
Portion of program where identifier can be used
There Two Scopes of Variables:
1. Global Variable:
Variable declared outside all the functions their scope comprise of the entire program
their lifetime ends when program is terminated
2. Local Variable:
Variable declared inside the function or within the braces has local scope can only be
visible inside the function or braces
Types of Storage Classes in C++
2. Static
3. Register
4. Extern
Automatic Storage Class
• The auto storage class is the default class for all variables declared
inside a block.
• It stands for automatic and all local variables declared in a block
automatically belong to this class.
1. Default Value = Garbage
2. Scope = Local
3. Memory Location = RAM
4. Lifetime = Till end of Scope
External Storage Class
• The extern storage class indicates that the variable is defined
elsewhere and has external linkage.
• It can be accessed between different files in a large program.
1. Default Value = Zero
2. Scope = Global
3. Memory Location = RAM
4. Lifetime = Till end of program
Static Storage Class
• Used to declare static variables
• Initialized only once and exist until program termination
• No new memory allocation for re-declaration
• Preserve their value even after going out of scope
• Global static variables can be accessed anywhere
1. Default Value = Zero
2. Scope = Local
3. Memory Location = RAM
4. Lifetime = Till end of program
Register Storage Class
• Declares register variables using the 'register' keyword
• Compiler tries to store variables in CPU register for faster access
• If no free register available, variables are stored in RAM
• Address of register variables cannot be obtained using pointers
1. Default Value = Garbage Value
2. Scope = Local
3. Memory Location = Register in CPU / RAM
4. Lifetime = Till end of its scope