PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Storage Classes in C: auto and register Variables (स्टोरेज क्लासेस: auto (डिफ़ॉल्ट लोकल) और register (CPU रजिस्टर)) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: In C, auto is the default local storage class with block scope, stack lifetime, and garbage initial value, while register hints the compiler to place a variable in a fast CPU register and forbids using the & address-of operator.
अध्याय सार: C में auto ब्लॉक-स्कोप वेरिएबल्स की डिफ़ॉल्ट स्टोरेज क्लास है जिसका मान अनिश्चित (Garbage) होता है, जबकि register कंपाइलर को वेरिएबल को तेज CPU रजिस्टर में रखने का सुझाव देता है और इस पर & ऑपरेटर वर्जित है।
Storage Classes in C: auto and register Variables: Detailed Notes & Architecture (स्टोरेज क्लासेस: auto (डिफ़ॉल्ट लोकल) और register (CPU रजिस्टर))
1. Four Properties of Storage Classes in C (C स्टोरेज क्लासेस के चार मुख्य निर्धारक गुण)
[EN] Every C variable declaration carries a Storage Class that determines four vital characteristics: Scope (where the identifier is visible—block vs file), Storage Duration or Lifetime (automatic block-level vs static program-wide), Linkage (none, internal, or external across translation units), and Default Initial Value (indeterminate/garbage vs zero). C defines four primary storage-class specifiers: auto, register, static, and extern (plus _Thread_local in C11).
[HI] C में प्रत्येक वेरिएबल की स्टोरेज क्लास चार मुख्य गुण तय करती है: स्कोप (कहाँ तक दिखाई देगा—ब्लॉक या फाइल), लाइफटाइम (कितनी देर मेमोरी में रहेगा), लिंकेज (एक या अनेक फाइलों में साझा होगा या नहीं), तथा डिफ़ॉल्ट प्रारंभिक मान (गार्बेज या शून्य)। C में चार मुख्य स्टोरेज क्लासेस हैं: auto, register, static और extern।
2. auto Storage Class: Default Block Scope & Automatic Lifetime (auto स्टोरेज क्लास: डिफ़ॉल्ट ब्लॉक स्कोप व जीवनकाल)
[EN] All local variables declared inside a function or curly-braced block { ... } default to the auto storage class even without writing the keyword. An auto variable has block scope, no linkage, and automatic storage duration: memory is allocated on the call stack when control enters the block and reclaimed when the block exits. Critically, an uninitialized auto variable holds an indeterminate (garbage) value—reading it before assignment causes Undefined Behavior. Placing auto at global/file scope is a compile-time error in C.
[HI] किसी फंक्शन या ब्लॉक { ... } के भीतर घोषित सभी लोकल वेरिएबल्स बिना कीवर्ड लिखे भी स्वतः auto श्रेणी के होते हैं। इनका स्कोप उसी ब्लॉक तक सीमित रहता है; ब्लॉक में प्रवेश पर स्टैक में स्थान मिलता है और बाहर निकलते ही नष्ट हो जाता है। बिना इनिशियलाइज किए auto वेरिएबल में अनिश्चित (Garbage) मान रहता है। ग्लोबल (फाइल) स्तर पर auto कीवर्ड लिखना C में सिंटैक्स एरर है।
3. register Storage Class: CPU Register Hint & Address Restriction (register स्टोरेज क्लास: CPU रजिस्टर संकेत व & प्रतिबंध)
[EN] The register keyword advises the compiler to store a frequently accessed local variable (such as a tight loop counter) directly inside a CPU hardware register instead of main RAM, avoiding memory bus load/store latency. However, register is only a hint—if hardware registers are exhausted, the compiler quietly falls back to stack memory. Regardless of where the compiler actually puts it, the C standard strictly forbids applying the unary address-of operator (&) to a register variable because CPU registers lack byte-addressable RAM addresses.
[HI] register कीवर्ड कंपाइलर से अनुरोध करता है कि बार-बार उपयोग होने वाले लोकल वेरिएबल (जैसे लूप काउंटर) को RAM के बजाय तेज CPU रजिस्टर में रखे। यह केवल एक सुझाव (Hint) है—रजिस्टर खाली न होने पर कंपाइलर इसे सामान्य मेमोरी में रख सकता है। फिर भी C नियम के अनुसार किसी भी register वेरिएबल पर एड्रेस-ऑफ ऑपरेटर (&) लगाना सख्त वर्जित (कम्पाइल एरर) है क्योंकि CPU रजिस्टरों का मेमोरी एड्रेस नहीं होता।
4. Usage Rules, Function Parameters & C vs C++ Differences (उपयोग नियम, पैरामीटर व C बनाम C++ अंतर)
[EN] Both auto and register are allowed only inside blocks (register is also legal on function parameters, e.g., void f(register int n)), and only one storage-class specifier may appear per declaration (writing auto register int x; is invalid). You may still apply sizeof to a register variable because sizeof inspects type size without taking an address. Notably, C++11 repurposed auto for type deduction and C++17 removed the register keyword entirely, whereas C keeps auto as a storage class (until C23 type-inference additions).
[HI] auto व register दोनों केवल ब्लॉक के भीतर वैध हैं (फंक्शन पैरामीटर में register मान्य है), और एक डिक्लेरेशन में दो स्टोरेज क्लासेस साथ नहीं लिख सकते। register वेरिएबल पर sizeof ऑपरेटर वैध है क्योंकि यह पता नहीं मांगता। ध्यान दें कि C++11 में auto डेटा टाइप अनुमान (Type Inference) के लिए बदल दिया गया और C++17 में register को हटा दिया गया, जबकि मानक C में यह स्टोरेज क्लास ही है।
Syntax, Formula & Technical Example (Storage Classes in C: auto and register Variables)
#include <stdio.h>
void demo(register int limit) {
auto int sum = 0; // Explicit auto (same as 'int sum = 0;')
for (register int i = 1; i <= limit; i++) {
sum += i; // &i would cause a compile error!
}
printf("Sum = %d, size = %zu\n", sum, sizeof(limit));
}
int main(void) { demo(5); return 0; }
Storage Classes in C: auto and register Variables Quick Reference & Comparison Table
| Storage Attribute | auto Specification | register Specification |
|---|---|---|
| Default Local Status | Implicit default for all block-scoped locals | Must be explicitly requested via register keyword |
| Storage Location Hint | Call Stack (automatic RAM storage) | CPU hardware register (falls back to RAM if full) |
| Address-of (&) Operator | Allowed (&var returns valid stack pointer) | Strictly forbidden in C (compile-time error) |
| Global File-Scope Usage | Compile error at file scope in C | Illegal at file scope (valid only in blocks/params) |
Storage Classes in C: auto and register Variables Top 20 Bilingual MCQs with Answers
Q1. What is the default storage class of a local variable declared as 'int x = 5;' inside a C function?
(किसी C फंक्शन के भीतर 'int x = 5;' के रूप में घोषित लोकल वेरिएबल की डिफ़ॉल्ट स्टोरेज क्लास क्या होती है?)
(A) auto
(B) register
(C) static
(D) extern
Correct Answer: (A)
[EN] Variables defined inside a block without an explicit storage-class specifier default to auto.
[HI] बिना किसी कीवर्ड के ब्लॉक के अंदर घोषित सभी लोकल वेरिएबल डिफ़ॉल्ट रूप से auto होते हैं।
Q2. What is the default initial value of an uninitialized auto variable in C?
(C में बिना इनिशियलाइज किए गए auto वेरिएबल का डिफ़ॉल्ट प्रारंभिक मान क्या होता है?)
(A) Negative one (-1)
(B) Indeterminate (Garbage) value
(C) NULL pointer
(D) Guaranteed 0
Correct Answer: (B)
[EN] Automatic stack variables are not zeroed out on block entry and therefore hold leftover stack bytes (garbage).
[HI] ब्लॉक में प्रवेश पर auto वेरिएबल शून्य से साफ नहीं किए जाते, अतः उनमें अनिश्चित (Garbage) मान होता है।
Q3. Which operation on a 'register int count = 10;' variable causes a compile-time error in ISO C?
('register int count = 10;' वेरिएबल पर कौन सा ऑपरेशन मानक ISO C में कम्पाइल-टाइम एरर उत्पन्न करता है?)
(A) Modifying it via count++
(B) Applying sizeof(count)
(C) Taking its memory address with &count
(D) Passing count by value to printf
Correct Answer: (C)
[EN] Because registers are not addressable RAM locations, C explicitly prohibits using unary & on register variables.
[HI] चूंकि CPU रजिस्टर RAM एड्रेस नहीं रखते, इसलिए C में register वेरिएबल के साथ & ऑपरेटर का प्रयोग वर्जित है।
Q4. What happens if a C function declares 50 'register' variables on a CPU with only 16 general-purpose registers?
(यदि किसी CPU में केवल 16 रजिस्टर हों और एक C फंक्शन 50 'register' वेरिएबल घोषित कर दे, तो क्या होगा?)
(A) Program crashes at runtime with Segmentation Fault
(B) Compilation immediately fails with OutOfRegisters error
(C) CPU creates virtual registers inside the hard disk
(D) Compiler puts up to available registers and stores the rest in normal RAM automatically
Correct Answer: (D)
[EN] register is purely an advisory optimization hint; excess register variables quietly degrade to auto storage in RAM.
[HI] register केवल एक अनुरोध है; अतिरिक्त वेरिएबल्स को कंपाइलर बिना किसी त्रुटि के सामान्य स्टैक मेमोरी में रख देता है।
Q5. Where in a C program is the 'register' storage class valid?
(C प्रोग्राम में 'register' स्टोरेज क्लास कहाँ-कहाँ उपयोग करना वैध है?)
(A) Only at global file scope
(B) Inside local blocks and as function parameters
(C) Only inside struct member declarations
(D) Both globally and locally
Correct Answer: (B)
[EN] C permits register on block-scoped variables and function parameters (e.g., void f(register int x)), NOT on globals or struct fields.
[HI] register केवल ब्लॉक वेरिएबल्स और फंक्शन पैरामीटर में मान्य है; ग्लोबल वेरिएबल या struct सदस्य पर यह अवैध है।
Q6. What is the result of compiling 'auto int g = 10;' outside all functions (at file scope) in C?
(C में सभी फंक्शनों के बाहर (फाइल स्कोप पर) 'auto int g = 10;' कम्पाइल करने का परिणाम क्या होगा?)
(A) Compile-time error (auto is illegal at file scope)
(B) g behaves identically to static int g = 10
(C) g becomes a global variable with external linkage
(D) g is stored in a CPU register
Correct Answer: (A)
[EN] Automatic storage duration cannot exist at file scope; GCC/Clang reject file-scope auto declarations.
[HI] ऑटोमैटिक जीवनकाल केवल ब्लॉक के अंदर संभव है, इसलिए ग्लोबल स्तर पर auto लिखने से कम्पाइल एरर आता है।
Q7. What happens when an inner block declares a new auto variable with the same name as an outer block variable?
(जब किसी आंतरिक ब्लॉक में बाहरी ब्लॉक के समान नाम वाला नया auto वेरिएबल घोषित किया जाता है, तो क्या होता है?)
(A) The inner variable becomes global
(B) Compilation fails due to duplicate symbol in the same function
(C) The inner declaration overwrites the outer variable permanently
(D) The inner variable shadows the outer one inside the inner block without altering the outer value
Correct Answer: (D)
[EN] C block scoping allows inner variables to hide outer ones of the same name; when the inner block exits, the outer value remains intact.
[HI] आंतरिक ब्लॉक का वेरिएबल केवल उस ब्लॉक के भीतर बाहरी वेरिएबल को ढक (Shadow) लेता है, और ब्लॉक समाप्त होने पर बाहरी मान सुरक्षित रहता है।
Q8. What does 'auto register int x = 5;' produce in C?
(C में 'auto register int x = 5;' लिखने पर क्या होता है?)
(A) x gets initialized to 0 instead of 5
(B) Compile-time error because at most one storage-class specifier is allowed
(C) auto overrides register silently
(D) x is stored simultaneously in stack and register
Correct Answer: (B)
[EN] C syntax restricts each declaration to a single storage-class specifier.
[HI] C सिंटैक्स के अनुसार एक डिक्लेरेशन में एक से अधिक स्टोरेज-क्लास कीवर्ड नहीं लगाए जा सकते।
Q9. Is 'sizeof(r)' valid when 'register double r = 2.5;' is declared in C?
(यदि C में 'register double r = 2.5;' घोषित हो, तो क्या 'sizeof(r)' लिखना वैध है?)
(A) Yes, sizeof is evaluated at compile time from the type without taking an address
(B) Only on 32-bit architectures
(C) Only if r is cast to int first
(D) No, because sizeof takes the runtime address of r
Correct Answer: (A)
[EN] sizeof inspects the operand's type (double -> 8 bytes) without evaluating or addressing the object, so it is completely legal.
[HI] sizeof केवल डेटा टाइप का आकार जांचता है और मेमोरी पता नहीं लेता, इसलिए यह register वेरिएबल पर पूरी तरह वैध है।
Q10. What is the lifetime (storage duration) of an auto variable declared inside a loop body?
(किसी लूप के अंदर घोषित auto वेरिएबल का जीवनकाल (Lifetime) कितना होता है?)
(A) From program startup until main() returns
(B) Until explicitly freed using free()
(C) Across all files in the project
(D) From block entry on each iteration until that iteration exits the closing brace
Correct Answer: (D)
[EN] Each pass through a block creates and destroys its automatic variables (or conceptually re-initializes them).
[HI] लूप के प्रत्येक चक्र में ब्लॉक में प्रवेश करने पर यह बनता है और समापन कोष्ठक पर नष्ट हो जाता है।
Q11. How does C++11/C++17 treat the 'auto' and 'register' keywords differently from traditional C?
(C++11/C++17 में 'auto' और 'register' कीवर्ड्स का व्यवहार पारंपरिक C से कैसे भिन्न है?)
(A) C++17 made register mandatory on all loop counters
(B) C++11 uses auto for type inference and C++17 removed register completely
(C) C++11 uses auto to allocate variables on the heap
(D) Both languages behave identically for auto and register
Correct Answer: (B)
[EN] In modern C++, auto deduces variable type from initializer (e.g., auto x = 3.14;), and register was removed in C++17.
[HI] आधुनिक C++ में auto टाइप अनुमान (Type Deduction) करता है और C++17 से register कीवर्ड को हटा दिया गया है।
Q12. Consider: void f() { int a = 1; { int a = 2; printf("%d ", a); } printf("%d", a); }. What is the output?
(कोड: void f() { int a = 1; { int a = 2; printf("%d ", a); } printf("%d", a); } का आउटपुट क्या होगा?)
(A) 2 2
(B) 1 1
(C) Compile error
(D) 2 1
Correct Answer: (D)
[EN] Inner auto a = 2 shadows outer a = 1 only inside the inner braces; outer a remains 1.
[HI] आंतरिक ब्लॉक में 2 प्रिंट होता है, और ब्लॉक खत्म होने के बाद बाहरी वेरिएबल का मान 1 प्रिंट होता है।
Q13. Which memory segment normally holds auto variables during execution in C?
(C प्रोग्राम के निष्पादन के दौरान auto वेरिएबल्स सामान्यतः किस मेमोरी सेगमेंट में रहते हैं?)
(A) Stack segment
(B) Uninitialized BSS segment
(C) Read-only Code/Text segment
(D) Dynamic Heap segment
Correct Answer: (A)
[EN] Automatic local variables reside in the function's stack frame on the Call Stack.
[HI] ऑटोमैटिक लोकल वेरिएबल कॉल स्टैक (Stack Segment) के फंक्शन फ्रेम में रहते हैं।
Q14. Why do modern optimizing C compilers often ignore the 'register' keyword?
(आधुनिक ऑप्टिमाइजिंग C कंपाइलर अक्सर 'register' कीवर्ड को अनदेखा क्यों करते हैं?)
(A) Variables cannot change value inside registers
(B) The register keyword forces code to run slower
(C) Modern CPUs no longer contain hardware registers
(D) Compiler graph-coloring register allocators outperform manual hints
Correct Answer: (D)
[EN] Modern compilers analyze variable liveness across the entire control-flow graph to assign registers far more effectively than programmers.
[HI] आधुनिक कंपाइलर ग्राफ-कलरिंग एल्गोरिदम से स्वतः सबसे बेहतर ढंग से रजिस्टर आवंटित कर लेते हैं।
Q15. Which statement is NOT true about auto and register variables in C?
(C में auto और register वेरिएबल्स के बारे में इनमें से कौन सा कथन असत्य है?)
(A) Applying & to a register variable in C is prohibited
(B) Uninitialized register variables are guaranteed by C to start at zero
(C) auto variables have block scope and no linkage
(D) Both auto and register have automatic storage duration
Correct Answer: (B)
[EN] Just like auto variables, uninitialized register variables have indeterminate (garbage) values—they are NOT zero-initialized.
[HI] auto की तरह ही बिना इनिशियलाइज किए गए register वेरिएबल का मान भी अनिश्चित (Garbage) होता है, शून्य नहीं।
Q16. Can a pointer variable itself be declared with the 'register' keyword in C (e.g., register int *p;)?
(क्या C में किसी पॉइंटर वेरिएबल को स्वयं 'register' कीवर्ड के साथ घोषित किया जा सकता है (जैसे register int *p;)?)
(A) Yes, and &p becomes legal too
(B) No, pointers can never use register
(C) Only void pointers can be register
(D) Yes, p is stored in a register, and *p is legal, though &p is forbidden
Correct Answer: (D)
[EN] The pointer variable p itself may live in a CPU register and be dereferenced (*p), though taking &p remains illegal.
[HI] हाँ, पॉइंटर वेरिएबल p स्वयं रजिस्टर में रह सकता है और *p से मान पढ़ सकता है, बस &p करना वर्जित रहता है।
Q17. Within Storage Classes in C: auto and register Variables, which storage attribute directly maps to: "Implicit default for all block-scoped locals"?
(Storage Classes in C: auto and register Variables में कौन सा Storage Attribute सीधे "Implicit default for all block-scoped locals" से संबंधित है?)
(A) Default Local Status
(B) Global File-Scope Usage
(C) Address-of (&) Operator
(D) Storage Location Hint
Correct Answer: (A)
[EN] In Storage Classes in C: auto and register Variables, 'Default Local Status' handles 'Implicit default for all block-scoped locals' (Must be explicitly requested via register keyword).
[HI] Storage Classes in C: auto and register Variables में 'Default Local Status' का कार्य/विवरण 'Implicit default for all block-scoped locals' (Must be explicitly requested via register keyword) है।
Q18. Which option accurately describes 'Storage Location Hint' in Storage Classes in C: auto and register Variables?
(Storage Classes in C: auto and register Variables के संदर्भ में 'Storage Location Hint' की सही विशेषता कौन सी है?)
(A) Implicit default for all block-scoped locals
(B) Call Stack (automatic RAM storage)
(C) Compile error at file scope in C
(D) Allowed (&var returns valid stack pointer)
Correct Answer: (B)
[EN] 'Storage Location Hint' provides 'Call Stack (automatic RAM storage)' — key point: CPU hardware register (falls back to RAM if full).
[HI] 'Storage Location Hint' का मुख्य विवरण 'Call Stack (automatic RAM storage)' है (CPU hardware register (falls back to RAM if full))।
Q19. In Storage Classes in C: auto and register Variables, identify the correct parameter for the specification: "Allowed (&var returns valid stack pointer)".
(Storage Classes in C: auto and register Variables में "Allowed (&var returns valid stack pointer)" विवरण किस विकल्प के लिए सही है?)
(A) Address-of (&) Operator
(B) Storage Location Hint
(C) Default Local Status
(D) Global File-Scope Usage
Correct Answer: (A)
[EN] 'Address-of (&) Operator' corresponds to 'Allowed (&var returns valid stack pointer)' (Strictly forbidden in C (compile-time error)).
[HI] 'Address-of (&) Operator' का संबंध 'Allowed (&var returns valid stack pointer)' (Strictly forbidden in C (compile-time error)) से है।
Q20. Considering Storage Classes in C: auto and register Variables, what key behavior defines 'Global File-Scope Usage'?
(Storage Classes in C: auto and register Variables में 'Global File-Scope Usage' की मुख्य भूमिका या मान क्या है?)
(A) Allowed (&var returns valid stack pointer)
(B) Compile error at file scope in C
(C) Call Stack (automatic RAM storage)
(D) Implicit default for all block-scoped locals
Correct Answer: (B)
[EN] 'Global File-Scope Usage' represents 'Compile error at file scope in C' (Illegal at file scope (valid only in blocks/params)).
[HI] 'Global File-Scope Usage' का अर्थ/मान 'Compile error at file scope in C' है (Illegal at file scope (valid only in blocks/params))।
Storage Classes in C: auto and register Variables — Exam FAQs
Q1: Why does C disallow '&x' on a register variable even when the compiler actually places it in RAM?
जब कंपाइलर रजिस्टर न मिलने पर register वेरिएबल को RAM में ही रख दे, तब भी C उस पर '&x' क्यों नहीं लगाने देता?
[EN] The C language grammar defines the restriction semantically at compile time based on the storage-class specifier, guaranteeing portability regardless of how many registers a target CPU happens to have.
[HI] C भाषा का नियम हार्डवेयर के बजाय सीधे कीवर्ड के आधार पर कम्पाइल समय में ही & को रोकता है ताकि प्रोग्राम हर प्रोसेसर आर्किटेक्चर पर एक समान व्यवहार करे।
Q2: Does declaring a variable 'auto' improve or hurt speed compared to omitting 'auto'?
किसी लोकल वेरिएबल के आगे 'auto' लिखने और न लिखने से गति पर क्या फर्क पड़ता है?
[EN] Zero difference. Inside any block, 'int x;' and 'auto int x;' generate identical machine code because auto is already the implicit default.
[HI] कोई अंतर नहीं पड़ता। ब्लॉक के भीतर 'int x;' और 'auto int x;' कंपाइल होकर बिल्कुल एक जैसा मशीन कोड बनाते हैं।
Q3: In C++, why can you take the address of a 'register' variable (before C++17)?
C++ में (C++17 से पहले) register वेरिएबल का एड्रेस (&) लेना क्यों संभव था जबकि C में नहीं?
[EN] Unlike C, older C++ standards permitted taking the address of a register variable by simply forcing the compiler to ignore the register hint and place that variable in addressable memory.
[HI] C के विपरीत पुराने C++ मानक में & लेने पर कंपाइलर स्वतः register सुझाव को रद्द करके उस वेरिएबल को RAM में रख देता था।
Post a Comment
Post a Comment