← Back to C Course | Chapter 14: Additional Topics | Lesson 4 of 9

C के Static Functions और Variables

static calls के बीच एक variable की value रखता है या एक file के अंदर एक नाम hide करता है, एक private notebook की तरह जो अपने room में रहती है। यह limit करता है कि कौन कुछ देख सकता है और यह कितनी देर रहता है।
Syntax
c
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

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

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

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

Static Functions

एक function को static declare करना इसकी visibility को इसके defined file तक restrict करता है, जो internal helper functions के लिए useful है जिन्हें दूसरी files के सीधे call करने का कोई legitimate कारण नहीं है, आपके project के externally usable API को छोटा और ज़्यादा intentional रखते हुए।

उदाहरण: Static Functions

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

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

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

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

c
// 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;
}
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. यह उम्मीद करना कि एक static local variable हर call पर reset होगा, जब यह अपनी value रखता है।
  2. किसी दूसरी file से एक static function call करने की कोशिश करना, जो fail होती है क्योंकि यह अपनी file के लिए private है।
  3. static global (file-private) को एक normal global से confuse करना जिसे दूसरी files extern से इस्तेमाल कर सकती हैं।

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.