The switch statement

Previous Table of Contents "New C Standard" commentary

1748 The controlling expression of a switch statement shall have integer type.

1749 If a switch statement has an associated case or default label within the scope of an identifier with a variably modified type, the entire switch statement shall be within the scope of that identifier.133)

1750 The expression of each case label shall be an integer constant expression and no two of the case constant expressions in the same switch statement shall have the same value after conversion.

1751 There may be at most one default label in a switch statement.

1752 (Any enclosed switch statement may have a default label or case constant expressions with values that duplicate case constant expressions in the enclosing switch statement.)

1753 A switch statement causes control to jump to, into, or past the statement that is the switch body, depending on the value of a controlling expression, and on the presence of a default label and the values of any case labels on or in the switch body.

1754 A case or default label is accessible only within the closest enclosing switch statement.

1755 The integer promotions are performed on the controlling expression.

1756 The constant expression in each case label is converted to the promoted type of the controlling expression.

1757 If a converted value matches that of the promoted controlling expression, control jumps to the statement following the matched case label.

1758 Otherwise, if there is a default label, control jumps to the labeled statement.

1759 If no converted case constant expression matches and there is no default label, no part of the switch body is executed.

1760 As discussed in, the implementation may limit the number of case values in a switch statement.

1761 133) That is, the declaration either precedes the switch statement, or it follows the last case or default label associated with the switch that is in the block containing the declaration.

1762 EXAMPLE In the artificial program fragment

        switch (expr)
                int i = 4;
        case 0:
                i = 17;
                /* falls through into default code */
                printf("%d\n", i);

the object whose identifier is i exists with automatic storage duration (within the block) but is never initialized, and thus if the controlling expression has a nonzero value, the call to the printf function will access an indeterminate value. Similarly, the call to the function f cannot be reached.


Created at: 2008-01-30 02:39:44 The text from WG14/N1256 is copyright © ISO