0% found this document useful (0 votes)
38 views7 pages

07 Scope and Storage Classess

The document discusses different types of storage classes and scopes of variables in C++. There are two scopes: global and local. Storage classes determine lifetime, visibility, default values, and storage location of variables. The main storage classes are automatic, static, extern, and register. Automatic variables are local and exist until the end of their scope. Extern variables can be accessed between files and exist until the end of the program. Static variables preserve their values between function calls and also exist until the end of the program. Register variables attempt to store in CPU registers for faster access.

Uploaded by

Raza Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views7 pages

07 Scope and Storage Classess

The document discusses different types of storage classes and scopes of variables in C++. There are two scopes: global and local. Storage classes determine lifetime, visibility, default values, and storage location of variables. The main storage classes are automatic, static, extern, and register. Automatic variables are local and exist until the end of their scope. Extern variables can be accessed between files and exist until the end of the program. Static variables preserve their values between function calls and also exist until the end of the program. Register variables attempt to store in CPU registers for faster access.

Uploaded by

Raza Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Scope and Storage Classes

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++

• It determines the lifetime, visibility, default value, and storage location


of a particular variable.

• There are following types of storage classes in C++


1. Automatic

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

You might also like