← Back to C++ Course | Chapter 5: Functions | Lesson 12 of 13

C++ में Scope और Lifetime

Scope वह जगह है जहाँ एक नाम देखा जा सकता है, सिर्फ एक room में फुसफुसाए जा सकने वाले एक secret की तरह। एक block के अंदर बने नाम block खत्म होने पर गायब हो जाते हैं।
Syntax
cpp
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

cpp
#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;
}

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

cpp
// 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;
}

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

cpp
#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;
}

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

cpp
#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;
}

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

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक local variable को उस block के बाहर इस्तेमाल करना जहाँ यह declared था।
  2. same नाम के एक local से एक global को shadow करना और गलत वाला बदल देना।
  3. यह उम्मीद करना कि एक normal local calls के बीच अपनी value रखेगा, जब यह static declared न होने पर हर call में reset होता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.