Storage Classes in C Language – auto, register, static and extern
What are Storage Classes in C?
In C programming language, a storage class specifies important properties of a variable or function, such as its scope, storage duration and linkage. These properties determine where an identifier can be accessed, how long an object exists and whether it can be referred to from other parts of a program. In traditional C programming, four commonly taught storage-class specifiers are auto, register, static and extern.
C प्रोग्रामिंग भाषा में Storage Class किसी variable या function के महत्वपूर्ण गुणों को निर्धारित करता है, जैसे उसका scope, storage duration और linkage। इन गुणों से पता चलता है कि किसी variable को प्रोग्राम के किस भाग में access किया जा सकता है, वह कितने समय तक अस्तित्व में रहता है और क्या उसे प्रोग्राम के अन्य भागों से refer किया जा सकता है। Traditional C programming में मुख्य रूप से चार storage-class specifiers पढ़ाए जाते हैं: auto, register, static और extern।
Types of Storage Classes in C
The four commonly taught storage classes are:
C भाषा में सामान्यतः निम्नलिखित चार storage classes का अध्ययन किया जाता है:
autoStorage ClassregisterStorage ClassstaticStorage ClassexternStorage Class
1. auto Storage Class in C
The auto storage class is the default storage class for ordinary local variables declared inside a block or function. Such variables have automatic storage duration, which means they are created when execution enters their block and their lifetime ends when execution leaves that block.
auto storage class का उपयोग block या function के अंदर declare किए गए सामान्य local variables के लिए किया जाता है। ऐसे variables का automatic storage duration होता है, अर्थात जब program control उस block में प्रवेश करता है, तब variable अस्तित्व में आता है और block से बाहर निकलने पर उसका lifetime समाप्त हो जाता है।
Syntax:
auto data_type variable_name;Example: Program to demonstrate auto storage class.
#include <stdio.h>int main(void){auto int a = 10;printf("Value of a = %d", a);return 0;}
Output:
Value of a = 10Explanation: In this program, the variable a is declared using the auto keyword. It is a local variable of the main() function and can be accessed within its scope. The auto keyword is optional because ordinary local variables are automatic by default.
व्याख्या: इस प्रोग्राम में a variable को auto keyword का उपयोग करके declare किया गया है। यह main() function का local variable है और इसे इसके scope के अंदर access किया जा सकता है। सामान्य local variables default रूप से automatic होते हैं, इसलिए auto keyword लिखना आवश्यक नहीं है।
Important Points:
The default storage class of ordinary local variables is
auto.An automatic local variable has block scope.
Its lifetime ends when execution leaves its block.
An uninitialized automatic variable has an indeterminate value.
सामान्य local variables की default storage class
autoहोती है।Automatic local variable का block scope होता है।
Block से बाहर निकलने पर इसका lifetime समाप्त हो जाता है।
बिना initialize किए गए automatic variable का मान indeterminate होता है।
2. register Storage Class in C
The register storage class is used to suggest that a variable may be accessed frequently and that the compiler may consider keeping it in a processor register for faster access. However, this is only a suggestion to the compiler; it does not guarantee that the variable will be stored in a CPU register.
register storage class का उपयोग compiler को यह सुझाव देने के लिए किया जाता है कि किसी variable को बार-बार access किया जा सकता है और तेज access के लिए compiler उसे processor register में रखने पर विचार कर सकता है। हालांकि, यह केवल compiler को दिया गया सुझाव है; इससे यह निश्चित नहीं होता कि variable वास्तव में CPU register में ही store होगा।
Syntax:
register data_type variable_name;Example: Program to demonstrate register storage class.
#include <stdio.h>int main(void){register int i;for(i = 1; i <= 5; i++){printf("%d ", i);}return 0;}
Output:
1 2 3 4 5Explanation: In this program, the variable i is declared using the register keyword. It is used as a loop-control variable. The compiler may use a processor register for this variable, but the actual decision depends on the compiler.
व्याख्या: इस प्रोग्राम में i variable को register keyword से declare किया गया है। इसका उपयोग loop-control variable के रूप में किया गया है। Compiler इस variable के लिए processor register का उपयोग कर सकता है, लेकिन वास्तविक निर्णय compiler पर निर्भर करता है।
Important Points:
registeris a request or suggestion to the compiler.It has automatic storage duration and block scope when declared as a local variable.
It does not guarantee faster execution.
In traditional C (C17 and earlier), the address-of operator
&cannot be applied to an object declared with theregisterstorage-class specifier.registercompiler को दिया गया एक request या suggestion है।Local variable के रूप में declare करने पर इसका automatic storage duration और block scope होता है।
इससे तेज execution की guarantee नहीं मिलती है।
Traditional C (C17 और उससे पहले) में
registerसे declare किए गए object पर address-of operator&का उपयोग नहीं किया जा सकता है।
3. static Storage Class in C
The static storage class gives a variable static storage duration. A static variable exists throughout the execution of the program and retains its value between function calls when declared inside a function. If it is not explicitly initialized, its initial value is zero.
static storage class किसी variable को static storage duration प्रदान करता है। Static variable पूरे program के execution के दौरान अस्तित्व में रहता है और यदि इसे किसी function के अंदर declare किया जाता है, तो यह अलग-अलग function calls के बीच अपना मान सुरक्षित रखता है। यदि इसे explicitly initialize नहीं किया जाता है, तो इसका प्रारंभिक मान zero होता है।
Syntax:
static data_type variable_name;Example: Program to demonstrate static storage class.
#include <stdio.h>void display(void){static int count = 0;count++;printf("%d ", count);}int main(void){display();display();display();return 0;}
Output:
1 2 3Explanation: In this program, count is declared as a static local variable inside the display() function. It is initialized only once and retains its value between function calls. Therefore, its value increases from 1 to 3 instead of being reset to zero on every call.
व्याख्या: इस प्रोग्राम में count को display() function के अंदर static local variable के रूप में declare किया गया है। यह केवल एक बार initialize होता है और अलग-अलग function calls के बीच अपना मान सुरक्षित रखता है। इसलिए प्रत्येक call पर इसका मान zero से शुरू नहीं होता, बल्कि 1 से 3 तक बढ़ता जाता है।
Important Points:
A static variable has static storage duration.
It retains its value throughout the execution of the program.
A static local variable has block scope.
A static local variable is initialized only once.
An uninitialized static variable is automatically initialized to zero.
A static variable declared at file scope has internal linkage, meaning it is accessible by name only within that source file.
Static variable का static storage duration होता है।
यह पूरे program execution के दौरान अपना मान सुरक्षित रखता है।
Static local variable का block scope होता है।
Static local variable केवल एक बार initialize होता है।
बिना initialize किए गए static variable का प्रारंभिक मान स्वतः zero होता है।
File scope पर declare किए गए static variable का internal linkage होता है, अर्थात उसे उसके नाम से केवल उसी source file के अंदर access किया जा सकता है।
4. extern Storage Class in C
The extern storage-class specifier is used to declare an object or function that is defined elsewhere. It allows a source file or another part of a program to refer to an object or function with external linkage. An extern declaration usually refers to an existing definition rather than creating a new object.
extern storage-class specifier का उपयोग ऐसे object या function को declare करने के लिए किया जाता है जिसकी definition किसी अन्य स्थान पर मौजूद होती है। यह program के किसी दूसरे भाग या source file को external linkage वाले object या function को refer करने की सुविधा देता है। सामान्यतः extern declaration किसी पहले से मौजूद definition को refer करता है, न कि नया object बनाता है।
Syntax:
extern data_type variable_name;Example: Program to demonstrate extern storage class.
#include <stdio.h>int num = 100;int main(void){extern int num;printf("Value of num = %d", num);return 0;}
Output:
Value of num = 100Explanation: In this program, the global variable num is defined outside the main() function. The declaration extern int num; refers to the existing variable. Therefore, the value 100 is displayed.
व्याख्या: इस प्रोग्राम में global variable num को main() function के बाहर define किया गया है। extern int num; declaration उसी पहले से मौजूद variable को refer करता है। इसलिए output में 100 display होता है।
Important Points:
externis used to declare variables or functions defined elsewhere.It is commonly used when sharing global variables between multiple source files.
An
externdeclaration generally does not allocate a new object.The actual definition of the variable must exist somewhere in the program.
An ordinary file-scope variable defined without
staticgenerally has external linkage.externका उपयोग अन्य स्थान पर define किए गए variables या functions को declare करने के लिए किया जाता है।इसका उपयोग सामान्यतः multiple source files के बीच global variables share करने के लिए किया जाता है।
सामान्यतः
externdeclaration नया object allocate नहीं करता है।Program में उस variable की वास्तविक definition कहीं न कहीं मौजूद होनी चाहिए।
बिना
staticके file scope पर define किया गया सामान्य variable सामान्यतः external linkage रखता है।
Difference Between auto, register, static and extern
The following table summarizes the main characteristics of the four storage-class specifiers.
नीचे दी गई table में चारों storage-class specifiers की मुख्य विशेषताओं को संक्षेप में समझाया गया है।
| Feature | auto | register | static (local) | extern |
|---|---|---|---|---|
| Storage duration | Automatic | Automatic | Static | Usually static for an object |
| Scope | Block | Block | Block | Depends on declaration |
| Default initial value | Indeterminate | Indeterminate | Zero | Defined object is zero if not initialized |
| Retains value between function calls | No | No | Yes | Yes, for the referred object |
| Main purpose | Local variables | Compiler optimization hint | Retaining values | Referring to external definitions |
extern keyword does not itself mean that a variable has global scope or that it always creates a new variable. Its exact effect depends on the declaration and the existing definition.Scope, storage duration और linkage अलग-अलग concepts हैं। extern keyword का अर्थ यह नहीं है कि variable हमेशा global scope रखता है या यह हमेशा नया variable बनाता है। इसका वास्तविक प्रभाव declaration और पहले से मौजूद definition पर निर्भर करता है।
Storage classes in C help programmers understand the scope, lifetime and linkage of variables and functions. The auto and register specifiers are associated with automatic storage duration, static provides static storage duration, and extern is commonly used to refer to definitions with external linkage. Understanding these concepts is important for writing structured and efficient C programs.
C भाषा में Storage Classes programmers को variables और functions के scope, lifetime और linkage को समझने में सहायता करते हैं। auto और register specifiers automatic storage duration से संबंधित हैं, static static storage duration प्रदान करता है और extern का उपयोग सामान्यतः external linkage वाली definitions को refer करने के लिए किया जाता है। इन concepts को समझना अच्छी तरह से structured और efficient C programs लिखने के लिए महत्वपूर्ण है।
Comments
Post a Comment