1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
<h1 id="firstHeading" class="firstHeading">switch statement</h1> <p>Executes code according to the value of an integral argument.</p>
<p>Used where one or several out of many branches of code need to be executed according to an integral value.</p>
<h3 id="Syntax"> Syntax</h3> <table class="t-sdsc-begin"> <tr class="t-sdsc"> <td class="t-sdsc-nopad"> <span class="t-spar">attr-spec-seq</span><span class="t-mark">(optional)</span> <code>switch (</code> <span class="t-spar">expression</span> <code>)</code> <span class="t-spar">statement</span> </td> <td class="t-sdsc-nopad"> </td> <td class="t-sdsc-nopad"> </td>
</tr>
</table> <table class="t-par-begin"> <tr class="t-par"> <td> <span class="t-spar">attr-spec-seq</span> </td> <td> - </td> <td> <span class="t-mark-rev t-since-c23">(C23)</span>optional list of <a href="attributes" title="c/language/attributes">attributes</a>, applied to the <code>switch</code> statement </td>
</tr> <tr class="t-par"> <td> <span class="t-spar">expression</span> </td> <td> - </td> <td> any <a href="expressions" title="c/language/expressions">expression</a> of <a href="type#Type_groups" title="c/language/type">integer type</a> (char, signed or unsigned integer, or enumeration) </td>
</tr> <tr class="t-par"> <td> <span class="t-spar">statement</span> </td> <td> - </td> <td> any <a href="statements" title="c/language/statements">statement</a> (typically a compound statement). <code>case:</code> and <code>default:</code> labels are permitted in <span class="t-spar">statement</span>, and <code>break;</code> statement has special meaning. </td>
</tr>
</table> <table class="t-sdsc-begin"> <tr class="t-sdsc"> <td> <code>case</code> <span class="t-spar">constant-expression</span> <code>:</code> <span class="t-spar">statement</span> </td> <td> (1) </td> <td> <span class="t-mark-rev t-until-c23">(until C23)</span> </td>
</tr> <tr class="t-sdsc"> <td> <span class="t-spar">attr-spec-seq</span><span class="t-mark">(optional)</span> <code>case</code> <span class="t-spar">constant-expression</span> <code>:</code> <span class="t-spar">statement</span><span class="t-mark">(optional)</span> </td> <td> (1) </td> <td> <span class="t-mark-rev t-since-c23">(since C23)</span> </td>
</tr> <tr class="t-sdsc"> <td> <code>default</code> <code>:</code> <span class="t-spar">statement</span> </td> <td> (2) </td> <td> <span class="t-mark-rev t-until-c23">(until C23)</span> </td>
</tr> <tr class="t-sdsc"> <td> <span class="t-spar">attr-spec-seq</span><span class="t-mark">(optional)</span> <code>default</code> <code>:</code> <span class="t-spar">statement</span><span class="t-mark">(optional)</span> </td> <td> (2) </td> <td> <span class="t-mark-rev t-since-c23">(since C23)</span> </td>
</tr>
</table> <table class="t-par-begin"> <tr class="t-par"> <td> <span class="t-spar">constant-expression</span> </td> <td> - </td> <td> any integer <a href="constant_expression" title="c/language/constant expression">constant expression</a> </td>
</tr> <tr class="t-par"> <td> <span class="t-spar">attr-spec-seq</span> </td> <td> - </td> <td> <span class="t-mark-rev t-since-c23">(C23)</span>optional list of <a href="attributes" title="c/language/attributes">attributes</a>, applied to the label </td>
</tr>
</table> <h3 id="Explanation"> Explanation</h3> <p>The body of a switch statement may have an arbitrary number of <code>case:</code> labels, as long as the values of all <span class="t-spar">constant-expressions</span> are unique (after <a href="conversion" title="c/language/conversion">conversion</a> to the <a href="conversion#Integer_promotions" title="c/language/conversion">promoted type</a> of <span class="t-spar">expression</span>). At most one <code>default:</code> label may be present (although nested switch statements may use their own <code>default:</code> labels or have <code>case:</code> labels whose constants are identical to the ones used in the enclosing switch).</p>
<p>If <span class="t-spar">expression</span> evaluates to the value that is equal to the value of one of <span class="t-spar">constant-expression</span>s after conversion to the promoted type of <span class="t-spar">expression</span>, then control is transferred to the statement that is labeled with that <span class="t-spar">constant-expression</span>.</p>
<p>If <span class="t-spar">expression</span> evaluates to a value that doesn't match any of the <code>case:</code> labels, and the <code>default:</code> label is present, control is transferred to the statement labeled with the <code>default:</code> label.</p>
<p>If <span class="t-spar">expression</span> evaluates to a value that doesn't match any of the <code>case:</code> labels, and the <code>default:</code> label is not present, none of the switch body is executed.</p>
<p>The <a href="break" title="c/language/break">break</a> statement, when encountered anywhere in <span class="t-spar">statement</span>, exits the switch statement:</p>
<div class="c source-c"><pre data-language="c">switch(1) {
case 1 : puts("1"); // prints "1",
case 2 : puts("2"); // then prints "2" ("fall-through")
}</pre></div> <div class="c source-c"><pre data-language="c">switch(1) {
case 1 : puts("1"); // prints "1"
break; // and exits the switch
case 2 : puts("2");
break;
}</pre></div> <table class="t-rev-begin"> <tr class="t-rev t-since-c99">
<td> <p>As with all other selection and iteration statements, the switch statement establishes <a href="scope" title="c/language/scope">block scope</a>: any identifier introduced in the <span class="t-spar">expression</span> goes out of scope after the <span class="t-spar">statement</span>.</p>
<p>If a VLA or another identifier with variably-modified type has a <code>case:</code> or a <code>default:</code> label within its scope, the entire switch statement must be in its scope (in other words, a VLA must be declared either before the entire switch or after the last label):</p>
<div class="c source-c"><pre data-language="c">switch (expr)
{
int i = 4; // not a VLA; OK to declare here
f(i); // never called
// int a[i]; // error: VLA cannot be declared here
case 0:
i = 17;
default:
int a[i]; // OK to declare VLA here
printf("%d\n", i); // prints 17 if expr == 0, prints indeterminate value otherwise
}</pre></div> </td> <td><span class="t-mark-rev t-since-c99">(since C99)</span></td>
</tr> </table> <h3 id="Keywords"> Keywords</h3> <p><a href="../keyword/switch" title="c/keyword/switch"><code>switch</code></a>, <a href="../keyword/case" title="c/keyword/case"><code>case</code></a>, <a href="../keyword/default" title="c/keyword/default"><code>default</code></a></p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <stdio.h>
void func(int x)
{
printf("func(%d): ", x);
switch(x)
{
case 1: printf("case 1, ");
case 2: printf("case 2, ");
case 3: printf("case 3.\n"); break;
case 4: printf("case 4, ");
case 5:
case 6: printf("case 5 or case 6, ");
default: printf("default.\n");
}
}
int main(void)
{
for(int i = 1; i < 9; ++i) func(i);
}</pre></div> <p>Output:</p>
<div class="text source-text"><pre data-language="c">func(1): case 1, case 2, case 3.
func(2): case 2, case 3.
func(3): case 3.
func(4): case 4, case 5 or case 6, default.
func(5): case 5 or case 6, default.
func(6): case 5 or case 6, default.
func(7): default.
func(8): default.</pre></div> </div> <h3 id="References"> References</h3> <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul><li> 6.8.4.2 The switch statement (p: 108-109) </li></ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 6.8.4.2 The switch statement (p: 149-150) </li></ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 6.8.4.2 The switch statement (p: 134-135) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 3.6.4.2 The switch statement </li></ul>
</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/language/switch" title="cpp/language/switch">C++ documentation</a></span> for <span class=""><span><code>switch</code> statement</span></span> </td>
</tr> </table> <div class="_attribution">
<p class="_attribution-p">
© cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
<a href="https://en.cppreference.com/w/c/language/switch" class="_attribution-link">https://en.cppreference.com/w/c/language/switch</a>
</p>
</div>
|