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 की local scope होती है, मतलब यह सिर्फ same function के अंदर के code को visible है -- दूसरे functions में code के पास इसे reference करने का कोई तरीका नहीं, same नाम से भी नहीं।
उदाहरण: Local Scope
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Global Scope
हर function के बाहर declared एक variable की global scope होती है, इसे program के किसी भी function को visible और modifiable बनाते हुए -- shared state के लिए convenient, लेकिन ज़्यादा risky क्योंकि कोई भी function इसकी value unexpectedly बदल सकता है।
उदाहरण: Global Scope
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Block Scope
एक if block या loop के अंदर declared variables की block scope होती है, मतलब वे सिर्फ उस specific curly braces के pair के अंदर exist करते हैं और accessible हैं, execution उस block को छोड़ते ही गायब हो जाते हैं।
उदाहरण: Block Scope
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Variable Lifetime
एक variable की lifetime यह है कि program के execution के दौरान इसकी memory allocation कितनी देर persist करती है -- एक local variable की memory उसके enclosing function के return होते ही automatically reclaim हो जाती है, और इसकी पुरानी value inaccessible हो जाती है।
उदाहरण: Variable Lifetime
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Variable Name Hiding
अगर एक local variable एक global variable के साथ नाम share करता है, C local वाले को उसकी अपनी scope के अंदर precedence लेने देता है, वहाँ effectively global की value hide करते हुए -- global unaffected रहता है और दूसरे functions से अभी भी accessible है।
उदाहरण: Variable Name Hiding
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- एक local variable को उस block के बाहर इस्तेमाल करना जहाँ यह declared था।
- same नाम के एक local से एक global को shadow करना और गलत वाला बदल देना।
- यह उम्मीद करना कि एक local variable calls के बीच अपनी value रखेगा, जब यह
staticdeclared न होने पर reset हो जाता है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: