C++ में Scope और Lifetime
In this page:
type global_name = value; // global scope
void function_name() {
type local_name = value; // local scope, exists only in this function
}
Local Scope
एक function के अंदर declared एक variable सिर्फ उस function के अंदर exist करता है — यह function शुरू होने पर बनता है और function के return होते ही destroy हो जाता है, इसे उस function के बाहर किसी भी code के लिए invisible और inaccessible बनाते हुए।
यह local scope है जो अलग functions को एक-दूसरे के temporary data में accidentally interfere करने से रोकता है।
उदाहरण: Local Scope
#include <iostream>
void showLocal() {
int local = 5;
std::cout << local << std::endl;
}
int main() {
showLocal();
// std::cout << local; // error: local doesn't exist here
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Global Scope
एक file के top level पर, हर function के बाहर declared एक variable की global scope है और इसे program में कहीं से भी पढ़ा या modify किया जा सकता है, program की पूरी runtime के लिए persist करते हुए।
Global variables genuinely shared state के लिए convenient हैं, लेकिन इन्हें overuse करना यह track करना मुश्किल बनाता है कि किस function ने कब एक value बदली।
उदाहरण: Global Scope
// Include std::cout and std::cin
#include <iostream>
// Declare globalCounter and set it to 0
int globalCounter = 0;
void increment() {
globalCounter++;
}
// Program execution starts in main()
int main() {
increment();
increment();
// Print to the console with cout
std::cout << globalCounter << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Block Scope
Curly braces के एक pair के अंदर declared कोई भी variable — चाहे यह एक if block हो, एक loop हो, या एक standalone block — उस block तक confined है और closing brace पार होते ही exist करना बंद कर देता है।
यह local scope का एक stricter version है, और यही एक loop variable i नाम को same function में कई अलग loops में safely reuse होने देता है।
उदाहरण: Block Scope
#include <iostream>
int main() {
if (true) {
int blockVar = 42;
std::cout << blockVar << std::endl;
}
// std::cout << blockVar; // error: blockVar is out of scope here
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Name Hiding
अगर एक local variable एक global variable के साथ नाम share करता है, local वाला इसकी अपनी scope के अंदर हर जगह priority लेता है, effectively उस local scope के खत्म होने तक global variable को view से hide करते हुए।
आप scope resolution operator, ::variableName, इस्तेमाल करके अभी भी explicitly hidden global access कर सकते हैं अगर आपको genuinely इसकी ज़रूरत हो।
उदाहरण: Name Hiding
#include <iostream>
int value = 100;
void showValue() {
int value = 5; // hides the global "value" inside this function
std::cout << value << std::endl;
}
int main() {
showValue();
std::cout << value << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Static Local Variables
एक local variable को static mark करना इसकी lifetime dramatically बदल देता है: हर function call पर destroy और recreate होने की बजाय, यह सिर्फ पहली बार function चलने पर initialize होता है और फिर हर बाद की call में अपनी value रखता है।
यह एक call counter जैसी किसी चीज़ के लिए useful है जिसे एक global variable बने बिना invocations के बीच persist करना ज़रूरी है।
उदाहरण: Static Local Variables
// Include std::cout and std::cin
#include <iostream>
void counter() {
static int count = 0;
count++;
// Print to the console with cout
std::cout << count << std::endl;
}
// Program execution starts in main()
int main() {
counter();
counter();
counter();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- एक local variable को उस block के बाहर इस्तेमाल करना जहाँ यह declared था।
- same नाम के एक local से एक global को shadow करना और गलत वाला बदल देना।
- यह उम्मीद करना कि एक normal local calls के बीच अपनी value रखेगा, जब यह
staticdeclared न होने पर हर call में reset होता है।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first:
- C++ Functions Introduction
- C++ Function Parameters
- C++ Multiple Function Parameters
- C++ Passing by Reference
- C++ Passing Structures to Functions
- C++ Return Values
- C++ Function Overloading
- C++ Default Arguments
- C++ Recursion
- C++ Inline Functions
- C++ Lambda Functions
- C++ Scope & Lifetime
- C++ Math Functions