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

C के Storage Classes

Storage classes decide करते हैं कि एक variable कितनी देर जीता है और इसे कहाँ देखा जा सकता है, अलग-अलग rules वाले अलग तरह के lockers की तरह। auto, static, register और extern choices हैं।
Syntax
c
auto data_type variable_name;      // default for locals
register data_type variable_name;  // hint: keep in a CPU register
static data_type variable_name;    // keeps its value between calls
extern data_type variable_name;    // defined elsewhere

Auto Storage Class

auto बिना explicitly बताए declare किए किसी भी local variable के लिए default storage class है -- यह current function call के लिए temporary memory allocate करता है और वह call खत्म होते ही automatically destroy हो जाता है।

उदाहरण: Auto Storage Class

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

Register Storage Class

register compiler से एक variable को regular memory (RAM) की बजाय सीधे एक CPU register में store करने को कहता है, तेज़ access पाने के लिए -- हालांकि modern compilers आमतौर पर इस optimization decision को hint की परवाह किए बिना खुद ही लेते हैं।

उदाहरण: Register Storage Class

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	register int i;
	// Loop: repeat while the condition holds
	for (i = 0; i < 3; i++) {
		// Print formatted text to the screen
		printf("%d ", i);
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Static Storage Class

static एक local variable की lifetime बदलता है ताकि यह same function की अलग calls के बीच अपनी value रखे, हर बार reset होने की बजाय -- एक counter जैसी चीज़ों के लिए useful जिसे कई invocations में persist करना ज़रूरी है।

उदाहरण: Static Storage Class

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

Extern Storage Class

extern declare करता है कि एक variable एक अलग source file में defined है, कई .c files को हर एक को अपनी अलग copy की ज़रूरत के बिना same global variable share और modify करने देते हुए।

उदाहरण: Extern Storage Class

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

Storage Classes चुनना

सही storage class चुनने का मतलब है intent को behavior से match करना: static एक ऐसी value के लिए जिसे calls के बीच persist करना ज़रूरी है, auto साधारण short-lived locals के लिए, और register सिर्फ tight loop counters पर शायद ही कभी ज़रूरी performance hint के तौर पर।

उदाहरण: Selecting Storage Classes

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
void counter() {
	static int persistent = 0;
	// Declare temporary and set it to 0
	int temporary = 0;
	persistent++;
	temporary++;
	// Print formatted text to the screen
	printf("%d %d ", persistent, temporary);
}
// Program execution starts in main()
int main() {
	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. register इस्तेमाल करना और फिर & से variable का address लेना, जो allowed नहीं है।
  2. यह उम्मीद करना कि एक auto variable calls के बीच अपनी value रखेगा।
  3. किसी भी file में definition के बिना extern इस्तेमाल करना, जो एक linker error cause करता है।
🔒

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.