C के Storage Classes
In this page:
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Register Storage Class
register compiler से एक variable को regular memory (RAM) की बजाय सीधे एक CPU register में store करने को कहता है, तेज़ access पाने के लिए -- हालांकि modern compilers आमतौर पर इस optimization decision को hint की परवाह किए बिना खुद ही लेते हैं।
उदाहरण: Register Storage Class
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Static Storage Class
static एक local variable की lifetime बदलता है ताकि यह same function की अलग calls के बीच अपनी value रखे, हर बार reset होने की बजाय -- एक counter जैसी चीज़ों के लिए useful जिसे कई invocations में persist करना ज़रूरी है।
उदाहरण: Static Storage Class
// 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
Extern Storage Class
extern declare करता है कि एक variable एक अलग source file में defined है, कई .c files को हर एक को अपनी अलग copy की ज़रूरत के बिना same global variable share और modify करने देते हुए।
उदाहरण: Extern Storage Class
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Storage Classes चुनना
सही storage class चुनने का मतलब है intent को behavior से match करना: static एक ऐसी value के लिए जिसे calls के बीच persist करना ज़रूरी है, auto साधारण short-lived locals के लिए, और register सिर्फ tight loop counters पर शायद ही कभी ज़रूरी performance hint के तौर पर।
उदाहरण: Selecting Storage Classes
// 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;
}
Login to try C/C++/Java/PHP code in the editor
registerइस्तेमाल करना और फिर&से variable का address लेना, जो allowed नहीं है।- यह उम्मीद करना कि एक
autovariable calls के बीच अपनी value रखेगा। - किसी भी file में definition के बिना
externइस्तेमाल करना, जो एक linker error cause करता है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: