switch Statement in C: case, break, default Rules Notes in Hindi & English : switch स्टेटमेंट: केस लेबल्स (int/char), break और default नियम और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम switch Statement in C: case, break, default Rules (switch स्टेटमेंट: केस लेबल्स (int/char), break और default नियम) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।

Overview: The switch statement performs multi-way branching by matching one integral controlling expression against unique compile-time constant case labels. Without an explicit break or return, execution falls through sequentially into subsequent case blocks until break or the closing brace is reached.

अध्याय सार: switch स्टेटमेंट एकल इंटीग्रल एक्सप्रेशन के मान का मिलान अद्वितीय कंपाइल-टाइम कॉन्स्टेंट case लेबल्स से करके बहु-दिशात्मक ब्रांचिंग करता है। किसी case के अंत में break न लगाने पर नियंत्रण क्रमिक रूप से अगले case ब्लॉक में प्रवेश कर जाता है, जिसे फॉल-थ्रू (Fall-through) कहते हैं।

switch Statement in C: case, break, default Rules: Detailed Notes & Architecture (switch स्टेटमेंट: केस लेबल्स (int/char), break और default नियम)

1. Controlling Expression Rules & Integral Promotions (कंट्रोलिंग एक्सप्रेशन व इंटीग्रल प्रमोशन नियम)

[EN] The expression inside switch (expr) must have an integer type (char, short, int, long, enum, or _Bool); floating-point types (float, double), pointers, and character strings (char*) are strictly disallowed in C. Before dispatching, C applies integer promotions to expr (promoting char or short to int) and converts every case constant to that promoted type. Each case label must be an integer constant expression computable at compile time—runtime variables and range syntax like case 1..5: are rejected.

[HI] switch (expr) के भीतर का एक्सप्रेशन केवल इंटीग्रल प्रकार (char, short, int, long, enum या _Bool) का ही हो सकता है; float, double, पॉइंटर या स्ट्रिंग की अनुमति नहीं है। तुलना से पहले C में expr पर इंटीग्रल प्रमोशन लागू होता है (जैसे char से int) और सभी case मान उसी टाइप में रूपांतरित होते हैं। प्रत्येक case लेबल कंपाइल-टाइम पर ज्ञात इंटीग्रल कॉन्स्टेंट होना आवश्यक है; वेरिएबल या रेंज (जैसे 1..5) मान्य नहीं होते।

2. Case Label Uniqueness & Fall-Through Mechanics (case लेबल विशिष्टता व फॉल-थ्रू कार्यप्रणाली)

[EN] No two case labels within the same switch block may evaluate to the same constant value; for instance, mixing case 'A': and case 65: under ASCII causes a compile-time duplicate case error. Case labels serve strictly as jump targets and may appear in any numerical order. After jumping to a matching case, execution continues downward sequentially across subsequent cases—known as fall-through—until encountering break, return, goto, or the closing brace, which allows multiple cases like vowels to share one handler.

[HI] एक ही switch ब्लॉक में दो case लेबल्स का मान समान नहीं हो सकता; जैसे ASCII प्रणाली में case 'A': और case 65: एक साथ लिखने पर डुप्लीकेट केस एरर आएगा। केस लेबल जंप टारगेट होते हैं और किसी भी क्रम में लिखे जा सकते हैं। एक बार सही case पर पहुंचने के बाद कंट्रोल break या return न मिलने तक नीचे के सभी cases को चलाता रहता है (फॉल-थ्रू), जिसका उपयोग कई विकल्पों के लिए एक साझा कोड लिखने में होता है।

3. Default Label Placement & Misspelling Pitfall (default लेबल का स्थान व टाइपो त्रुटि)

[EN] The optional default: label handles any value that matches no explicit case, and at most one default: is permitted per switch. Although typically written last, ISO C allows placing default: at the top or middle of the switch body; if placed above other cases without a trailing break, a default hit falls through into the cases below it. A classic compiler trap occurs when misspelling default as 'defualt:'—C treats it silently as a valid goto label and compiles without error while bypassing default handling.

[HI] वैकल्पिक default: लेबल तब चलता है जब कोई भी case मेल न खाए, और एक switch में अधिकतम एक ही default: हो सकता है। यद्यपि इसे सामान्यतः अंत में लिखते हैं, मानक C में इसे switch के ऊपर या बीच में कहीं भी रखा जा सकता है; यदि इसके बाद break न हो तो यह भी नीचे के cases में फॉल-थ्रू करेगा। एक रोचक ट्रैप यह है कि default की स्पेलिंग 'defualt:' गलत लिखने पर C इसे goto लेबल मानकर बिना एरर कंपाइल कर देता है।

4. Declaration Scope, Skipped Initializers & Jump Tables (डिक्लेरेशन स्कोप, स्किप्ड इनिशियलाइजर व जंप टेबल)

[EN] Any variable initializer placed directly inside switch before the first case (e.g., switch(x) { int a = 5; case 1: ... }) is bypassed when execution jumps straight to case 1, leaving 'a' uninitialized even though it stays in scope. Furthermore, prior to C23, placing a declaration right after 'case 1:' without braces caused a syntax error because labels attached only to statements; wrapping a case in { ... } creates a compound block with safe local variables. Dense integer cases let compilers generate O(1) jump tables instead of linear comparisons.

[HI] पहले case से ठीक पहले switch ब्लॉक में घोषित व इनिशियलाइज किया गया वेरिएबल (जैसे int a = 5;) सीधे case 1 पर जंप होने के कारण इनिशियलाइज नहीं हो पाता और उसमें गार्बेज मान रहता है। साथ ही C23 से पहले सीधे 'case 1:' के तुरंत बाद बिना { } ब्रेसिज़ के नया वेरिएबल घोषित करना सिंटैक्स एरर देता था, इसलिए केस ब्लॉक में { ... } लगाना सुरक्षित रहता है। पास-पास इंटीजर मान होने पर कंपाइलर O(1) जंप टेबल बनाकर तेज निष्पादन करता है।

Syntax, Formula & Technical Example (switch Statement in C: case, break, default Rules)

int main(void) {
    char op = '+'; int a = 12, b = 4;
    switch (op) {
        case '+': printf("%d\n", a + b); break; // Prints 16
        case '-': printf("%d\n", a - b); break;
        case '*': case 'x': printf("%d\n", a * b); break;
        default:  printf("Invalid operator\n");
    }
    return 0;
}

switch Statement in C: case, break, default Rules Quick Reference & Comparison Table

switch ElementISO C SpecificationCommon Pitfall / Note
switch (expr)Accepts integer, char, or enum expression onlyfloat, double, and strings trigger compile error
case constant:Requires unique compile-time integer constantVariables, ranges (1..5), or 'A' with 65 fail
break keywordExits only the innermost enclosing switch/loopOmission causes fall-through into next case block
default labelOptional single fallback valid in any positionTop placement without break falls through downward

switch Statement in C: case, break, default Rules Top 20 Bilingual MCQs with Answers

Q1. Which data type is strictly forbidden as the controlling expression inside switch(expr) in standard C?

(मानक C में switch(expr) के भीतर इनमें से किस डेटा टाइप का उपयोग पूरी तरह वर्जित है?)

(A) enum

(B) char

(C) float or double

(D) long int

Correct Answer: (C)

[EN] switch in C only works on integral types (char, short, int, long, enum, _Bool); floating-point types and strings fail to compile.

[HI] C में switch केवल इंटीग्रल टाइप्स (char, int, long, enum) स्वीकार करता है; float, double या स्ट्रिंग पर कंपाइल एरर आता है।

Q2. Assuming 'int y = 5;' is a local variable, which case label inside switch(x) is valid in ISO C?

(यदि 'int y = 5;' एक लोकल वेरिएबल है, तो switch(x) के भीतर इनमें से कौन सा case लेबल मानक C में वैध है?)

(A) case y:

(B) case 5.0:

(C) case "5":

(D) case 2 + 3:

Correct Answer: (D)

[EN] 2 + 3 is a compile-time integer constant expression, whereas runtime variables, string literals, and floats are invalid.

[HI] 2 + 3 एक कंपाइल-टाइम इंटीग्रल कॉन्स्टेंट एक्सप्रेशन है, जबकि वेरिएबल, स्ट्रिंग लिटरल और फ्लोट अमान्य हैं।

Q3. What is the output of: int x = 2; switch(x) { case 1: printf("1"); case 2: printf("2"); case 3: printf("3"); default: printf("D"); }?

(कोड 'int x = 2; switch(x) { case 1: printf("1"); case 2: printf("2"); case 3: printf("3"); default: printf("D"); }' का आउटपुट क्या होगा?)

(A) 23

(B) 23D

(C) 2

(D) D

Correct Answer: (B)

[EN] Without break statements, matching case 2 prints '2' and falls straight through case 3 and default, printing '23D'.

[HI] break न होने के कारण कंट्रोल case 2 पर आकर '2' प्रिंट करता है और फिर क्रमशः case 3 तथा default में गिरकर '23D' प्रिंट करता है।

Q4. Where can the 'default:' label legally be placed inside a switch block in C?

(C में 'default:' लेबल को switch ब्लॉक के भीतर कहाँ रखा जा सकता है?)

(A) Strictly as the very last label only

(B) Anywhere inside the switch block (beginning, middle, or end)

(C) Outside the switch closing brace

(D) Strictly as the very first label only

Correct Answer: (B)

[EN] ISO C allows default: in any position inside switch, though if placed before cases it needs a break to prevent fall-through.

[HI] मानक C में default: को switch के भीतर ऊपर, बीच में या नीचे कहीं भी लिखा जा सकता है (परंतु ऊपर लिखने पर break जरूरी होगा)।

Q5. What is printed by: int x = 9; switch(x) { default: printf("D"); case 1: printf("1"); break; case 2: printf("2"); }?

(कोड 'int x = 9; switch(x) { default: printf("D"); case 1: printf("1"); break; case 2: printf("2"); }' का आउटपुट क्या होगा?)

(A) D12

(B) D

(C) Compile-time error

(D) D1

Correct Answer: (D)

[EN] 9 matches default, prints 'D', and since default lacks a break it falls through into case 1, prints '1', then breaks.

[HI] 9 के लिए default चलकर 'D' प्रिंट होता है और break न होने से यह case 1 में गिरकर '1' प्रिंट करने के बाद रुकता है।

Q6. On an ASCII machine, what happens if a single switch contains both 'case 'A':' and 'case 65:'?

(ASCII प्रणाली पर एक ही switch में 'case 'A':' और 'case 65:' दोनों लिखने पर क्या होगा?)

(A) Compile-time error due to duplicate case value

(B) Runtime warning only

(C) Only the first one runs

(D) Both blocks execute together

Correct Answer: (A)

[EN] Character constant 'A' has integer value 65 in ASCII, making both labels identical constants which C strictly forbids.

[HI] ASCII में 'A' का मान 65 होता है; एक ही switch में दो समान कॉन्स्टेंट केस लेबल कंपाइल एरर उत्पन्न करते हैं।

Q7. What happens if a C programmer accidentally misspells 'default:' as 'defualt:' inside a switch block?

(यदि प्रोग्रामर switch ब्लॉक में गलती से 'default:' की जगह 'defualt:' लिख दे, तो क्या होगा?)

(A) The compiler throws an unrecognized case error

(B) It compiles silently as an unused goto label and never runs as a default branch

(C) It triggers a segmentation fault

(D) It still executes as the default branch

Correct Answer: (B)

[EN] Because identifier followed by colon (defualt:) is valid C label syntax for goto, the compiler accepts it silently!

[HI] कॉलन लगा कोई भी पहचानकर्ता C में वैध goto लेबल होता है, अतः 'defualt:' बिना किसी सिंटैक्स एरर के कंपाइल हो जाएगा पर डिफ़ॉल्ट ब्रांच नहीं बनेगा।

Q8. In 'switch(1) { int a = 10; case 1: printf("%d", a); break; }', what happens to the initializer '= 10'?

('switch(1) { int a = 10; case 1: printf("%d", a); break; }' में '= 10' इनिशियलाइजेशन का क्या होता है?)

(A) a becomes 0 automatically

(B) The compiler refuses to allow any declaration before case 1

(C) 'a' is guaranteed to print 10

(D) The initialization is skipped when jumping to case 1, leaving 'a' uninitialized

Correct Answer: (D)

[EN] Control jumps directly to case 1, bypassing 'a = 10', so reading 'a' accesses an indeterminate/garbage value.

[HI] कंट्रोल सीधे case 1 पर कूदता है जिससे 'a = 10' लाइन छूट जाती है और 'a' में अनिश्चित (Garbage) मान रहता है।

Q9. When a 'break;' statement executes inside an inner switch nested inside an outer switch, what terminates?

(एक बाहरी switch के भीतर स्थित आंतरिक switch में 'break;' चलने पर क्या समाप्त होता है?)

(A) Only the inner switch terminates; execution continues inside the outer switch

(B) The enclosing function returns

(C) Both inner and outer switches terminate immediately

(D) Only the current case statement skips one line

Correct Answer: (A)

[EN] break always exits only the single innermost enclosing loop or switch statement.

[HI] break केवल अपने ठीक सबसे नजदीकी आंतरिक switch या लूप से ही बाहर निकालता है, बाहरी switch से नहीं।

Q10. What occurs if 'continue;' is placed directly inside a switch statement that is NOT inside any loop?

(यदि बिना किसी लूप के सीधे switch स्टेटमेंट के अंदर 'continue;' लिख दिया जाए, तो क्या होगा?)

(A) It skips to the next case label

(B) It acts identically to break

(C) Compile-time error because continue only applies to loops

(D) It jumps back to re-evaluate the switch expression

Correct Answer: (C)

[EN] Unlike break, the continue keyword is valid only inside iteration loops (while, for, do-while), never standalone switch.

[HI] break के विपरीत continue कीवर्ड केवल लूप्स (for, while, do-while) में काम करता है; अकेले switch में यह कंपाइल एरर देता है।

Q11. If a switch statement sits inside a 'for' loop and a matching case executes 'continue;', where does control jump?

(यदि किसी 'for' लूप के अंदर switch हो और एक case में 'continue;' चले, तो कंट्रोल कहाँ जाएगा?)

(A) To the update expression of the enclosing for loop

(B) Out of the for loop completely

(C) To the default label of the switch

(D) To the next case label inside the switch

Correct Answer: (A)

[EN] continue ignores switch boundaries and directly triggers the next iteration of the enclosing for loop (jumping to update).

[HI] continue अपने switch को अनदेखा करके बाहरी for लूप के अपडेट एक्सप्रेशन पर जंप करता है ताकि अगला चक्र शुरू हो।

Q12. If switch(5) contains only 'case 1:' and 'case 2:' with no default label, what happens?

(यदि switch(5) में केवल 'case 1:' और 'case 2:' हों तथा कोई default लेबल न हो, तो क्या होगा?)

(A) Nothing inside switch executes and control passes to the statement after switch

(B) Compile-time error for missing default

(C) A runtime exception is thrown

(D) case 2 runs automatically

Correct Answer: (A)

[EN] Both default and case labels are optional; if nothing matches and default is absent, the switch body is skipped.

[HI] default वैकल्पिक होता है; कोई केस न मिलने पर switch का पूरा ब्लॉक छोड़कर प्रोग्राम आगे बढ़ जाता है।

Q13. Why did 'case 1: int y = 5; break;' trigger a syntax error in C99/C11 unless wrapped in braces '{ int y = 5; break; }'?

(C99/C11 में बिना ब्रेसिज़ के 'case 1: int y = 5; break;' लिखने पर सिंटैक्स एरर क्यों आता था?)

(A) case labels prohibit initialized variables

(B) Labels in C99/C11 attach only to statements, not declarations (until C23)

(C) break cannot follow a variable declaration

(D) Integer variables cannot be named y inside switch

Correct Answer: (B)

[EN] Wrapping the block in { } forms a compound statement so declarations inside the case have clean block scope and valid grammar.

[HI] C99/C11 व्याकरण में लेबल सीधे डिक्लेरेशन से पहले नहीं आ सकते (केवल स्टेटमेंट से पहले); { } लगाने से वह कंपाउंड स्टेटमेंट बन जाता है।

Q14. How do optimizing C compilers typically implement a switch with 30 dense integer cases (0 to 29)?

(0 से 29 तक लगातार 30 इंटीजर केसों वाले switch को आधुनिक C कंपाइलर सामान्यतः किस संरचना से ऑप्टिमाइज करते हैं?)

(A) An O(1) jump table (indirect lookup)

(B) Dynamic runtime hashing

(C) An O(n^2) nested loop

(D) Sequential float conversions

Correct Answer: (A)

[EN] Dense integral cases allow the compiler to store target addresses in an array (jump table) for O(1) direct dispatch.

[HI] पास-पास स्थित इंटीजर केसों के लिए कंपाइलर पतों की एक सारणी (जंप टेबल) बना देता है जिससे सीधे O(1) में छलांग लगती है।

Q15. Which statement is NOT true about the switch statement in C?

(C में switch स्टेटमेंट के बारे में इनमें से कौन सा कथन असत्य है?)

(A) Multiple case labels can share a single code block via fall-through

(B) Case labels can test relational ranges like 'case x > 10:' directly

(C) At most one default label is allowed per switch

(D) Integer promotion applies to char expressions inside switch

Correct Answer: (B)

[EN] Standard C case labels only test constant equality; relational inequalities are forbidden.

[HI] मानक C में case लेबल्स केवल कॉन्स्टेंट समानता की जांच करते हैं; रेंज या असमानता शर्तें अवैध हैं।

Q16. In a switch checking character 'ch', how do you execute one shared block for both 'y' and 'Y' cleanly?

(कैरेक्टर 'ch' के switch में 'y' और 'Y' दोनों के लिए एक ही ब्लॉक चलाने का सही तरीका क्या है?)

(A) Write 'case 'y' || 'Y': /* code */ break;'

(B) Write 'case 'y': break; case 'Y':'

(C) Write 'case 'y': case 'Y': /* code */ break;'

(D) Write 'case 'y', 'Y': /* code */ break;'

Correct Answer: (C)

[EN] Stacking case labels relies on intentional fall-through; 'y' || 'Y' would evaluate to constant 1 and 'y', 'Y' evaluates only to 'Y'!

[HI] case 'y': case 'Y': लिखने से दोनों मान साझा कोड पर आते हैं, जबकि 'y' || 'Y' का मान 1 बन जाता है और कॉमा ऑपरेटर केवल 'Y' लेता है।

Q17. Within switch Statement in C: case, break, default Rules, which switch element directly maps to: "Accepts integer, char, or enum expression only"?

(switch Statement in C: case, break, default Rules में कौन सा switch Element सीधे "Accepts integer, char, or enum expression only" से संबंधित है?)

(A) case constant:

(B) break keyword

(C) switch (expr)

(D) default label

Correct Answer: (C)

[EN] In switch Statement in C: case, break, default Rules, 'switch (expr)' handles 'Accepts integer, char, or enum expression only' (float, double, and strings trigger compile error).

[HI] switch Statement in C: case, break, default Rules में 'switch (expr)' का कार्य/विवरण 'Accepts integer, char, or enum expression only' (float, double, and strings trigger compile error) है।

Q18. Which option accurately describes 'case constant:' in switch Statement in C: case, break, default Rules?

(switch Statement in C: case, break, default Rules के संदर्भ में 'case constant:' की सही विशेषता कौन सी है?)

(A) Accepts integer, char, or enum expression only

(B) Optional single fallback valid in any position

(C) Exits only the innermost enclosing switch/loop

(D) Requires unique compile-time integer constant

Correct Answer: (D)

[EN] 'case constant:' provides 'Requires unique compile-time integer constant' — key point: Variables, ranges (1..5), or 'A' with 65 fail.

[HI] 'case constant:' का मुख्य विवरण 'Requires unique compile-time integer constant' है (Variables, ranges (1..5), or 'A' with 65 fail)।

Q19. In switch Statement in C: case, break, default Rules, identify the correct parameter for the specification: "Exits only the innermost enclosing switch/loop".

(switch Statement in C: case, break, default Rules में "Exits only the innermost enclosing switch/loop" विवरण किस विकल्प के लिए सही है?)

(A) break keyword

(B) switch (expr)

(C) default label

(D) case constant:

Correct Answer: (A)

[EN] 'break keyword' corresponds to 'Exits only the innermost enclosing switch/loop' (Omission causes fall-through into next case block).

[HI] 'break keyword' का संबंध 'Exits only the innermost enclosing switch/loop' (Omission causes fall-through into next case block) से है।

Q20. Considering switch Statement in C: case, break, default Rules, what key behavior defines 'default label'?

(switch Statement in C: case, break, default Rules में 'default label' की मुख्य भूमिका या मान क्या है?)

(A) Requires unique compile-time integer constant

(B) Exits only the innermost enclosing switch/loop

(C) Accepts integer, char, or enum expression only

(D) Optional single fallback valid in any position

Correct Answer: (D)

[EN] 'default label' represents 'Optional single fallback valid in any position' (Top placement without break falls through downward).

[HI] 'default label' का अर्थ/मान 'Optional single fallback valid in any position' है (Top placement without break falls through downward)।

switch Statement in C: case, break, default Rules — Exam FAQs

Q1: Why cannot float, double, or strings be used in a C switch statement?

C के switch स्टेटमेंट में float, double या स्ट्रिंग का उपयोग क्यों नहीं किया जा सकता?

[EN] switch relies on exact discrete integer matching so compilers can build O(1) jump tables; floating-point numbers suffer precision rounding and C strings are character pointers compared by address rather than contents.

[HI] switch सटीक इंटीजर समानता पर आधारित होता है ताकि O(1) जंप टेबल बनाई जा सके; फ्लोटिंग-पॉइंट में परिशुद्धता त्रुटि हो सकती है और C स्ट्रिंग्स वास्तव में पॉइंटर्स होते हैं जिनकी तुलना विषय-वस्तु से नहीं पते से होती है।

Q2: Does the default branch always have to appear at the bottom of a switch statement?

क्या default शाखा को हमेशा switch के सबसे अंत में ही लिखना आवश्यक है?

[EN] No. Standard C allows default: anywhere inside the switch block (top, middle, or bottom). However, if placed above other case labels, you must add a break statement after it so it does not fall through into the following cases.

[HI] नहीं। मानक C में default: को switch ब्लॉक में कहीं भी (शुरुआत, मध्य या अंत में) लिखा जा सकता है, बशर्ते ऊपर लिखने पर नीचे के केसों में फॉल-थ्रू रोकने के लिए break लगाया जाए।

Q3: How do you safely declare a local variable inside a specific case block?

किसी विशेष case ब्लॉक के भीतर नया लोकल वेरिएबल सुरक्षित रूप से कैसे घोषित करें?

[EN] Enclose that case's statements inside curly braces { ... } to form a compound statement, which satisfies C99/C11 grammar and limits the variable's scope strictly to that case.

[HI] उस case के कोड को { ... } ब्रेसिज़ में बंद कर दें; इससे कंपाउंड स्टेटमेंट बनता है, C99/C11 सिंटैक्स एरर टलता है और वेरिएबल का स्कोप उसी case तक सीमित रहता है।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)