🔵 Core C++ · Lesson 33
Scope and Storage Classes
Scope and Storage Classes
What is Scope and Storage Classes?
Scope and Storage Classes
Scope defines where a variable can be accessed. Storage class affects lifetime, visibility and storage location of variables.
Scope defines where a variable can be accessed. Storage class affects lifetime, visibility and storage location of variables.
Level
🔵 Core C++ Features
🔵 Core C++ Features
Example File
scope-storage.cppMain Focus
Concept + syntax + practical C++ program
Concept + syntax + practical C++ program
Why should you learn this?
- It helps you write correct and readable C++ programs.
- It is used repeatedly in school practicals, projects and competitive programming.
- It builds the base for advanced topics such as OOP, STL and data structures.
Important Terms
| Term | Meaning / Use |
|---|---|
| local variable | Local variable used in Scope and Storage Classes programming. |
| global variable | Global variable used in Scope and Storage Classes programming. |
| static | Static used in Scope and Storage Classes programming. |
| extern | Extern used in Scope and Storage Classes programming. |
| lifetime | Lifetime used in Scope and Storage Classes programming. |
Syntax / Pattern
Variables declared inside function are local; static local variables retain value between calls.
Example Program
#include <iostream>
using namespace std;
void counter(){
static int c = 0;
c++;
cout << c << " ";
}
int main(){
counter(); counter(); counter();
return 0;
}
Expected Output
1 2 3
Program Explanation
- static variable is initialized once.
- It retains value between function calls.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.
Where will you use it?
- counters
- configuration
- multi-file projects
Common Mistakes
- Overusing global variables.
- Expecting local variable to exist after function ends.
Practice Tasks
- Compare local and global variable.
- Use static variable to count function calls.
Summary
Scope and Storage Classes is an important C++ topic. Learn the definition, understand the syntax, run the example program and then solve the practice tasks to make the concept strong.
What is Scope and Storage Classes?
Scope and Storage Classes
Scope defines where a variable can be accessed. Storage class affects lifetime, visibility and storage location of variables.
Scope defines where a variable can be accessed. Storage class affects lifetime, visibility and storage location of variables.
Level
🔵 Core C++ Features
🔵 Core C++ Features
Example File
scope-storage.cppMain Focus
Concept + syntax + practical C++ program
Concept + syntax + practical C++ program
Why should you learn this?
- It helps you write correct and readable C++ programs.
- It is used repeatedly in school practicals, projects and competitive programming.
- It builds the base for advanced topics such as OOP, STL and data structures.
Important Terms
| Term | Meaning / Use |
|---|---|
| local variable | Local variable used in Scope and Storage Classes programming. |
| global variable | Global variable used in Scope and Storage Classes programming. |
| static | Static used in Scope and Storage Classes programming. |
| extern | Extern used in Scope and Storage Classes programming. |
| lifetime | Lifetime used in Scope and Storage Classes programming. |
Syntax / Pattern
Variables declared inside function are local; static local variables retain value between calls.
Example Program
#include <iostream>
using namespace std;
void counter(){
static int c = 0;
c++;
cout << c << " ";
}
int main(){
counter(); counter(); counter();
return 0;
}
Expected Output
1 2 3
Program Explanation
- static variable is initialized once.
- It retains value between function calls.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.
Where will you use it?
- counters
- configuration
- multi-file projects
Common Mistakes
- Overusing global variables.
- Expecting local variable to exist after function ends.
Practice Tasks
- Compare local and global variable.
- Use static variable to count function calls.
Summary
Scope and Storage Classes is an important C++ topic. Learn the definition, understand the syntax, run the example program and then solve the practice tasks to make the concept strong.