Scope Rules in C Language with Examples
What is Scope in C Language?
In C language, scope refers to the region of a program where a declared identifier (such as a variable or function name) can be accessed or used. The scope of an identifier determines where it is visible in the program.
C लैंग्वेज में Scope का अर्थ प्रोग्राम के उस क्षेत्र से है जहाँ किसी डिक्लेअर किए गए आइडेंटिफायर (जैसे वेरिएबल या फंक्शन का नाम) को एक्सेस या उपयोग किया जा सकता है। किसी आइडेंटिफायर का स्कोप यह निर्धारित करता है कि प्रोग्राम में उसे कहाँ उपयोग किया जा सकता है।
In C language, there are four types of scope:
Block Scope
File Scope
Function Scope
Function Prototype Scope
C लैंग्वेज में स्कोप मुख्यतः चार प्रकार के होते हैं:
ब्लॉक स्कोप
फाइल स्कोप
फंक्शन स्कोप
फंक्शन प्रोटोटाइप स्कोप
1. Block Scope (Local Variables)
An identifier declared inside a block, such as inside a function or a pair of curly braces { }, has block scope. It can be accessed only from its point of declaration to the end of that block, subject to the language's visibility rules.
किसी ब्लॉक के अंदर डिक्लेअर किया गया आइडेंटिफायर, जैसे किसी फंक्शन या कर्ली ब्रेसेस { } के अंदर घोषित वेरिएबल, Block Scope रखता है। इसे अपनी डिक्लेरेशन के स्थान से उस ब्लॉक के अंत तक एक्सेस किया जा सकता है।
Variables declared inside a function or block are commonly called local variables. They cannot be accessed directly outside their respective block.
फंक्शन या ब्लॉक के अंदर डिक्लेअर किए गए वेरिएबल को सामान्यतः Local Variables कहा जाता है। इन्हें इनके संबंधित ब्लॉक के बाहर सीधे एक्सेस नहीं किया जा सकता है।
Example:
#include <stdio.h>int main(void){int a = 10;{int b = 20;printf("a = %d\n", a);printf("b = %d\n", b);}printf("a = %d\n", a);return 0;}
Output:
a = 10b = 20a = 10
Explanation: Variable a is declared in the outer block, so it can be accessed inside the inner block as well. Variable b is declared inside the inner block, so it cannot be accessed outside that block.
व्याख्या: वेरिएबल a बाहरी ब्लॉक में डिक्लेअर किया गया है, इसलिए इसे आंतरिक ब्लॉक में भी एक्सेस किया जा सकता है। वेरिएबल b आंतरिक ब्लॉक में डिक्लेअर किया गया है, इसलिए इसे उस ब्लॉक के बाहर एक्सेस नहीं किया जा सकता है।
2. File Scope (Global Variables)
An identifier declared outside all functions and blocks has file scope. Such variables are commonly called global variables. Their scope starts from their declaration and continues to the end of the source file, subject to declaration and linkage rules.
सभी फंक्शन और ब्लॉक के बाहर डिक्लेअर किए गए आइडेंटिफायर का File Scope होता है। ऐसे वेरिएबल को सामान्यतः Global Variables कहा जाता है। इनका स्कोप डिक्लेरेशन से लेकर सोर्स फाइल के अंत तक रहता है, जो डिक्लेरेशन और लिंकेज के नियमों पर निर्भर करता है।
Global variables can be accessed by functions appearing after their declaration in the same source file. A global variable with external linkage can also be accessed from another source file using an appropriate extern declaration.
ग्लोबल वेरिएबल को उसी सोर्स फाइल में उनके डिक्लेरेशन के बाद आने वाले फंक्शन्स द्वारा एक्सेस किया जा सकता है। External Linkage वाले ग्लोबल वेरिएबल को उचित extern डिक्लेरेशन के माध्यम से दूसरी सोर्स फाइल में भी एक्सेस किया जा सकता है।
Example:
#include <stdio.h>int a = 100; // Global variablevoid display(void){printf("Value of a = %d\n", a);}int main(void){printf("Value of a = %d\n", a);display();return 0;}
Output:
Value of a = 100Value of a = 100
Explanation: Variable a is declared outside all functions. Therefore, it can be accessed by both main() and display().
व्याख्या: वेरिएबल a सभी फंक्शन्स के बाहर डिक्लेअर किया गया है। इसलिए इसे main() और display() दोनों फंक्शन्स में एक्सेस किया जा सकता है।
Important Point: A global variable with static storage duration exists throughout program execution. File scope and lifetime are different concepts.
महत्वपूर्ण बात: Static Storage Duration वाला ग्लोबल वेरिएबल पूरे प्रोग्राम के एक्जीक्यूशन के दौरान अस्तित्व में रहता है। File Scope और Lifetime अलग-अलग कॉन्सेप्ट हैं।
3. Function Scope
In C language, function scope applies to labels used with the goto statement. A label can be accessed from anywhere within the same function, even if it is declared inside a nested block.
C लैंग्वेज में Function Scope का उपयोग goto स्टेटमेंट के साथ प्रयोग किए जाने वाले Labels पर लागू होता है। किसी Label को उसी फंक्शन के अंदर कहीं से भी उपयोग किया जा सकता है, भले ही उसे किसी Nested Block में डिक्लेअर किया गया हो।
Example:
#include <stdio.h>int main(void){int i = 1;if (i == 1){goto message;message:printf("Hello C Programming\n");}return 0;}
Output:
Hello C ProgrammingExplanation: The label message has function scope. The goto statement transfers control to that label within the same function.
व्याख्या: Label message का Function Scope होता है। goto स्टेटमेंट उसी फंक्शन के अंदर कंट्रोल को इस Label पर ट्रांसफर करता है।
4. Function Prototype Scope
The identifiers declared inside the parameter list of a function prototype have function prototype scope. Their scope ends at the end of the function prototype declaration.
किसी फंक्शन प्रोटोटाइप की पैरामीटर लिस्ट में डिक्लेअर किए गए आइडेंटिफायर का Function Prototype Scope होता है। इनका स्कोप फंक्शन प्रोटोटाइप डिक्लेरेशन के अंत में समाप्त हो जाता है।
Example:
#include <stdio.h>int add(int a, int b);int main(void){printf("Sum = %d\n", add(10, 20));return 0;}int add(int x, int y){return x + y;}
Output:
Sum = 30Explanation: The names a and b in the function prototype are only used within that prototype. The function definition uses its own parameter names, x and y.
व्याख्या: फंक्शन प्रोटोटाइप में दिए गए a और b नाम केवल उसी प्रोटोटाइप तक सीमित होते हैं। फंक्शन की डेफिनिशन में अपने अलग पैरामीटर नाम x और y उपयोग किए गए हैं।
Local Variables and Global Variables
The following table shows the main differences between local and global variables.
निम्न टेबल में Local और Global Variables के बीच मुख्य अंतर दिए गए हैं।
| Local Variables | Global Variables |
|---|---|
| Declared inside a function or block. | Declared outside all functions and blocks. |
| Have block scope. | Have file scope. |
| Accessible within their respective block. | Accessible after declaration in the same file, subject to visibility rules. |
| Automatic local variables have automatic storage duration. | Global variables have static storage duration. |
| Uninitialized automatic variables have indeterminate values. | Uninitialized global variables are initialized to zero. |
Actual Arguments and Formal Arguments
Actual arguments and formal arguments are related to function calling, not separate types of scope.
Actual Arguments are the values or expressions supplied in a function call. Formal Arguments (parameters) are the variables declared in the function definition to receive those values.
Actual Arguments और Formal Arguments का संबंध Function Calling से होता है, ये स्कोप के अलग-अलग प्रकार नहीं हैं।
Actual Arguments वे वैल्यू या एक्सप्रेशन्स होते हैं जो फंक्शन कॉल करते समय दिए जाते हैं। Formal Arguments या Parameters वे वेरिएबल होते हैं जो फंक्शन डेफिनिशन में इन वैल्यू को प्राप्त करने के लिए डिक्लेअर किए जाते हैं।
Example:
#include <stdio.h>int add(int a, int b){return a + b;}int main(void){int x = 10, y = 20;printf("Sum = %d\n", add(x, y));return 0;}
Output:
Sum = 30Explanation:
xandyare actual arguments.aandbare formal parameters.Parameters
aandbhave block scope within the function body.
व्याख्या:
xऔरyActual Arguments हैं।aऔरbFormal Parameters हैं।Parameters
aऔरbका Block Scope फंक्शन बॉडी के अंदर होता है।
Variable Shadowing in C
When a variable declared in an inner block has the same name as a variable declared in an outer block, the inner variable hides the outer variable within its scope. This is called variable shadowing.
जब किसी Inner Block में डिक्लेअर किए गए वेरिएबल का नाम Outer Block में डिक्लेअर किए गए वेरिएबल के समान होता है, तो Inner Variable अपने स्कोप के अंदर Outer Variable को छिपा देता है। इसे Variable Shadowing कहा जाता है।
Example:
#include <stdio.h>int main(void){int a = 10;{int a = 20;printf("Inner a = %d\n", a);}printf("Outer a = %d\n", a);return 0;}
Output:
Inner a = 20Outer a = 10
Explanation: The inner variable a hides the outer variable a inside the inner block. Outside the inner block, the outer variable becomes accessible again.
व्याख्या: Inner Block के अंदर मौजूद वेरिएबल a, Outer Block के वेरिएबल a को छिपा देता है। Inner Block के बाहर आने पर Outer Variable फिर से एक्सेस किया जा सकता है।
Scope rules in C language determine where variables, functions and other identifiers can be accessed in a program. Understanding block scope, file scope, function scope and function prototype scope helps programmers write well-organized and error-free programs.
C लैंग्वेज में Scope Rules यह निर्धारित करते हैं कि प्रोग्राम में वेरिएबल, फंक्शन और अन्य आइडेंटिफायर को कहाँ एक्सेस किया जा सकता है। Block Scope, File Scope, Function Scope और Function Prototype Scope को समझने से प्रोग्रामर व्यवस्थित और त्रुटिरहित प्रोग्राम लिख सकते हैं.
Comments
Post a Comment