← Back to C Course | Chapter 5: Functions | Lesson 6 of 8

C में Scope और Lifetime

Scope वह जगह है जहाँ एक नाम देखा जा सकता है, सिर्फ एक room में फुसफुसाए जा सकने वाले एक secret की तरह। एक block के अंदर बने variables इसके खत्म होने पर गायब हो जाते हैं।
Syntax
c
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 की local scope होती है, मतलब यह सिर्फ same function के अंदर के code को visible है -- दूसरे functions में code के पास इसे reference करने का कोई तरीका नहीं, same नाम से भी नहीं।

उदाहरण: Local Scope

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
void show() {
	// Declare local and set it to 5
	int local = 5;
	// Print formatted text to the screen
	printf("%d", local);
}
// Program execution starts in main()
int main() {
	show();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Global Scope

हर function के बाहर declared एक variable की global scope होती है, इसे program के किसी भी function को visible और modifiable बनाते हुए -- shared state के लिए convenient, लेकिन ज़्यादा risky क्योंकि कोई भी function इसकी value unexpectedly बदल सकता है।

उदाहरण: Global Scope

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Declare counter and set it to 10
int counter = 10;
void show() {
	// Print formatted text to the screen
	printf("%d", counter);
}
// Program execution starts in main()
int main() {
	show();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Block Scope

एक if block या loop के अंदर declared variables की block scope होती है, मतलब वे सिर्फ उस specific curly braces के pair के अंदर exist करते हैं और accessible हैं, execution उस block को छोड़ते ही गायब हो जाते हैं।

उदाहरण: Block Scope

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Check whether 1
	if (1) {
		// Declare x and set it to 5
		int x = 5;
		// Print formatted text to the screen
		printf("%d", x);
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Variable Lifetime

एक variable की lifetime यह है कि program के execution के दौरान इसकी memory allocation कितनी देर persist करती है -- एक local variable की memory उसके enclosing function के return होते ही automatically reclaim हो जाती है, और इसकी पुरानी value inaccessible हो जाती है।

उदाहरण: Variable Lifetime

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
void show() {
	// Declare x and set it to 5
	int x = 5;
	// Print formatted text to the screen
	printf("%d", x);
}
// Program execution starts in main()
int main() {
	show();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Variable Name Hiding

अगर एक local variable एक global variable के साथ नाम share करता है, C local वाले को उसकी अपनी scope के अंदर precedence लेने देता है, वहाँ effectively global की value hide करते हुए -- global unaffected रहता है और दूसरे functions से अभी भी accessible है।

उदाहरण: Variable Name Hiding

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Declare value and set it to 100
int value = 100;
void show() {
	// Declare value and set it to 5
	int value = 5;
	// Print formatted text to the screen
	printf("%d", value);
}
// Program execution starts in main()
int main() {
	show();
	// 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. यह उम्मीद करना कि एक local variable calls के बीच अपनी value रखेगा, जब यह static declared न होने पर reset हो जाता है।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

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.