C के Static Functions और Variables
In this page:
return_type function_name() {
static type variable = initial_value; // keeps its value between calls
}
static type global_variable; // visible only in this file
static return_type helper(void) { } // private to this file
Static Local Variables
एक static local variable सिर्फ पहली बार initialize होता है जब इसकी declaration पर पहुंचा जाए और फिर function की बाद की calls के बीच अपनी value रखता है, एक ordinary local variable के उलट जो function चलने पर हर बार scratch से recreate होता है (और अपनी पिछली value खो देता है)।
उदाहरण: Static Local Variables
// Include standard input/output (printf, scanf)
#include <stdio.h>
void counter() {
static int count = 0;
count++;
// Print formatted text to the screen
printf("%d ", count);
}
// 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
Static Global Variables
एक static global variable सिर्फ उस source file के अंदर visible है जहाँ यह declared है, मतलब same project की दूसरी .c files इसे access या accidentally modify नहीं कर सकतीं भले ही वे exact same नाम का एक variable declare करें — static keyword इसे program-wide linkage की बजाय file-level (internal) linkage देता है।
उदाहरण: Static Global Variables
// Include standard input/output (printf, scanf)
#include <stdio.h>
static int fileScoped = 42;
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d", fileScoped);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Static Functions
एक function को static declare करना इसकी visibility को इसके defined file तक restrict करता है, जो internal helper functions के लिए useful है जिन्हें दूसरी files के सीधे call करने का कोई legitimate कारण नहीं है, आपके project के externally usable API को छोटा और ज़्यादा intentional रखते हुए।
उदाहरण: Static Functions
// Include standard input/output (printf, scanf)
#include <stdio.h>
static int helper(int n) {
// Send n * 2 back to the caller
return n * 2;
}
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d", helper(5));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Static के साथ Scope Isolation
क्योंकि static functions और static global variables अपनी file के बाहर invisible हैं, same project की दो अलग .c files एक identical नाम वाला एक static function या variable safely declare कर सकती हैं बिना linker के कभी उनके बीच एक naming conflict report किए।
उदाहरण: Scope Isolation with Static
// Include standard input/output (printf, scanf)
#include <stdio.h>
static int value = 10;
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d", value);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Static Variables का Memory Lifetime
Static variables stack की बजाय program के data segment में रहते हैं, मतलब वे program start होने पर exactly एक बार allocate होते हैं और उन्हें रखने वाला function कितनी भी बार call हो, program की पूरी runtime के लिए वह memory occupy करते रहते हैं।
उदाहरण: Memory Lifetime of Static Variables
// Include standard input/output (printf, scanf)
#include <stdio.h>
void increment() {
static int total = 0;
total++;
// Print formatted text to the screen
printf("%d ", total);
}
// Program execution starts in main()
int main() {
increment();
increment();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- यह उम्मीद करना कि एक
staticlocal variable हर call पर reset होगा, जब यह अपनी value रखता है। - किसी दूसरी file से एक
staticfunction call करने की कोशिश करना, जो fail होती है क्योंकि यह अपनी file के लिए private है। staticglobal (file-private) को एक normal global से confuse करना जिसे दूसरी filesexternसे इस्तेमाल कर सकती हैं।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: