非常教程

C参考手册

C 语法

switch statement

根据整数参数的值执行代码。

用于需要根据整数值执行许多代码分支中的一个或多个分支的情况。

句法

开关(表达式)语句

表达

-

整数类型的任何表达式(char,signed或unsigned integer或枚举)

声明

-

任何陈述(通常是复合陈述)。情况:和默认值:标签允许在声明中,并打破; 声明有特殊的含义。

case constant_expression:语句

(1)

默认:语句

(2)

constant_expression

-

任何整数常量表达式

说明

case:只要所有constant_expressions的值都是唯一的(在转换为提升的表达式类型之后),switch语句的主体可以具有任意数量的标签。最多default:只能有一个标签(尽管嵌套的开关语句可能使用自己的default:标签,或者case:标签的常数与封闭开关中使用的常数相同)。

如果在转换为提升的表达式类型后,表达式求值为等于constant_expressions之一的值,那么控制权将转移到标有该constant_expression的语句。

如果表达式计算出的值与任何case:标签都不匹配,并且default:标签存在,则控件将转移到标签标签所对应的语句中default:

如果表达式求值的值与任何case:标签都不匹配,且default:标签不存在,则不会执行任何开关主体。

break语句在遇到语句中的任何地方时会退出switch语句:

switch(1) {
    case 1 : puts("1"); // prints "1",
    case 2 : puts("2"); // then prints "2" ("fall-through")
}
switch(1) {
    case 1 : puts("1"); // prints "1"
             break;     // and exits the switch
    case 2 : puts("2");
             break;
}

与所有其他选择和迭代语句一样,switch语句建立块范围:表达式中引入的任何标识符在语句后超出范围。如果一个VLA或其他具有不同修改类型的标识符在其范围内有一个case或者default标签,则整个switch语句必须在其范围内(换句话说,VLA必须在整个交换机之前或之后最后一个标签):switch(expr){int i = 4; //不是VLA; 可以在这里声明f(i); //永远不会调用// int ai; //错误:这里不能声明VLA案例0:i = 17; 默认:; int ai; //可以在这里声明VLA printf(“%d \ n”,i); //如果expr == 0则打印17,否则打印不确定的值}

(自C99以来)

关键词

switch, case, default.

#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: printf("case 5, ");
      default: printf("default.\n");
   }
}
 
int main(void)
{
   for(int i = 1; i < 10; ++i) func(i);
}

输出:

func(1): case 1, case 2, case 3.
func(2): case 2, case 3.
func(3): case 3.
func(4): case 4, case 5, default.
func(5): case 5, default.
func(6): default.
func(7): default.
func(8): default.
func(9): default.

参考

  • C11标准(ISO / IEC 9899:2011):
    • 6.8.4.2 switch语句(p:149-150)
  • C99标准(ISO / IEC 9899:1999):
    • 6.8.4.2 switch语句(p:134-135)
  • C89 / C90标准(ISO / IEC 9899:1990):
    • 3.6.4.2 switch语句

C 语法相关

1.#define directive
2.#elif directive
3.#else directive
4.#endif directive
5.#error directive
6.#if directive
7.#ifdef directive
8.#ifndef directive
9.#include directive
10.#line directive
11.#pragma directive
12.alignas
13.Alternative operators and tokens
14.Analyzability
15.Arithmetic operators
16.Arithmetic types
17.Array declaration
18.Array initialization
19.ASCII Chart
20.Assignment operators
21. types
22.Basic concepts
23.Bit fields
24.break statement
25.C language
26.C Operator Precedence
27.cast operator
28.character constant
29.Comments
30.Comparison operators
31.compound literals
32.Conditional inclusion
33.Conformance
34.const type qualifier
35.Constant expressions
36.continue statement
37.Declarations
38.do-while loop
39.Enumerations
40.Escape sequences
41.Expressions
42.External and tentative definitions
43.File scope
44.floating constant
45.for loop
46.Function declarations
47.Function definitions
48.Functions
49.Generic selection
50.goto statement
51.Identifier
52.if statement
53.Implicit conversions
54.Increment/decrement operators
55.Initialization
56.inline function specifier
57.integer constant
58.Lifetime
59.Logical operators
60.Lookup and name spaces
61.Main function
62.Member access operators
63.Memory model
64.Objects and alignment
65.Order of evaluation
66.Other operators
67.Phases of translation
68.Pointer declaration
69.Preprocessor
70.restrict type qualifier
71.return statement
72.Scalar initialization
73.Scope
74.sizeof operator
75.Statements
76.static assert declaration
77.Static storage duration
78.Storage-class specifiers
79.string literals
80.Struct and union initialization
81.Struct declaration
82.Thread storage duration
83.Type
84.Type
85.Typedef declaration
86.Undefined behavior
87.Union declaration
88.Value categories
89.Variadic arguments
90.volatile type qualifier
91.while loop
92._Alignof operator
93._Noreturn function specifier
C

C 语言是一门通用计算机编程语言,应用广泛。C 语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。