Стандарт языка Си С99 TC (1113411), страница 30
Текст из файла (страница 30)
A case ordefault label is accessible only within the closest enclosing switch statement.5The integer promotions are performed on the controlling expression. The constantexpression in each case label is converted to the promoted type of the controllingexpression. If a converted value matches that of the promoted controlling expression,control jumps to the statement following the matched case label. Otherwise, if there isa default label, control jumps to the labeled statement. If no converted case constantexpression matches and there is no default label, no part of the switch body isexecuted.Implementation limits6As discussed in 5.2.4.1, the implementation may limit the number of case values in aswitch statement.135) That is, the declaration either precedes the switch statement, or it follows the last case ordefault label associated with the switch that is in the block containing the declaration.134Language§6.8.4.2WG14/N12567EXAMPLECommittee Draft — Septermber 7, 2007ISO/IEC 9899:TC3In the artificial program fragmentswitch (expr){int i = 4;f(i);case 0:i = 17;/* falls through into default code */default:printf("%d\n", i);}the object whose identifier is i exists with automatic storage duration (within the block) but is neverinitialized, and thus if the controlling expression has a nonzero value, the call to the printf function willaccess an indeterminate value.
Similarly, the call to the function f cannot be reached.6.8.5 Iteration statementsSyntax1iteration-statement:while ( expression ) statementdo statement while ( expression ) ;for ( expressionopt ; expressionopt ; expressionopt ) statementfor ( declaration expressionopt ; expressionopt ) statementConstraints2The controlling expression of an iteration statement shall have scalar type.3The declaration part of a for statement shall only declare identifiers for objects havingstorage class auto or register.Semantics4An iteration statement causes a statement called the loop body to be executed repeatedlyuntil the controlling expression compares equal to 0.
The repetition occurs regardless ofwhether the loop body is entered from the iteration statement or by a jump.136)5An iteration statement is a block whose scope is a strict subset of the scope of itsenclosing block. The loop body is also a block whose scope is a strict subset of the scopeof the iteration statement.136) Code jumped over is not executed. In particular, the controlling expression of a for or whilestatement is not evaluated before entering the loop body, nor is clause-1 of a for statement.§6.8.5Language135ISO/IEC 9899:TC3Committee Draft — Septermber 7, 2007WG14/N12566.8.5.1 The while statement1The evaluation of the controlling expression takes place before each execution of the loopbody.6.8.5.2 The do statement1The evaluation of the controlling expression takes place after each execution of the loopbody.6.8.5.3 The for statement1The statementfor ( clause-1 ; expression-2 ; expression-3 ) statementbehaves as follows: The expression expression-2 is the controlling expression that isevaluated before each execution of the loop body.
The expression expression-3 isevaluated as a void expression after each execution of the loop body. If clause-1 is adeclaration, the scope of any identifiers it declares is the remainder of the declaration andthe entire loop, including the other two expressions; it is reached in the order of executionbefore the first evaluation of the controlling expression. If clause-1 is an expression, it isevaluated as a void expression before the first evaluation of the controlling expression.137)2Both clause-1 and expression-3 can be omitted.
An omitted expression-2 is replaced by anonzero constant.6.8.6 Jump statementsSyntax1jump-statement:goto identifier ;continue ;break ;return expressionopt ;Semantics2A jump statement causes an unconditional jump to another place.137) Thus, clause-1 specifies initialization for the loop, possibly declaring one or more variables for use inthe loop; the controlling expression, expression-2, specifies an evaluation made before each iteration,such that execution of the loop continues until the expression compares equal to 0; and expression-3specifies an operation (such as incrementing) that is performed after each iteration.136Language§6.8.6WG14/N1256Committee Draft — Septermber 7, 2007ISO/IEC 9899:TC36.8.6.1 The goto statementConstraints1The identifier in a goto statement shall name a label located somewhere in the enclosingfunction.
A goto statement shall not jump from outside the scope of an identifier havinga variably modified type to inside the scope of that identifier.Semantics2A goto statement causes an unconditional jump to the statement prefixed by the namedlabel in the enclosing function.3EXAMPLE 1 It is sometimes convenient to jump into the middle of a complicated set of statements. Thefollowing outline presents one possible approach to a problem based on these three assumptions:1.The general initialization code accesses objects only visible to the current function.2.The general initialization code is too large to warrant duplication.3.The code to determine the next operation is at the head of the loop.
(To allow it to be reached bycontinue statements, for example.)/* ... */goto first_time;for (;;) {// determine next operation/* ... */if (need to reinitialize) {// reinitialize-only code/* ... */first_time:// general initialization code/* ... */continue;}// handle other operations/* ... */}§6.8.6.1Language137ISO/IEC 9899:TC34Committee Draft — Septermber 7, 2007WG14/N1256EXAMPLE 2 A goto statement is not allowed to jump past any declarations of objects with variablymodified types. A jump within the scope, however, is permitted.// invalid: going INTO scope of VLA.goto lab3;{double a[n];a[j] = 4.4;lab3:a[j] = 3.3;goto lab4;a[j] = 5.5;lab4:a[j] = 6.6;}goto lab4;// valid: going WITHIN scope of VLA.// invalid: going INTO scope of VLA.6.8.6.2 The continue statementConstraints1A continue statement shall appear only in or as a loop body.Semantics2A continue statement causes a jump to the loop-continuation portion of the smallestenclosing iteration statement; that is, to the end of the loop body.
More precisely, in eachof the statementswhile (/* ... */) {/* ... */continue;/* ... */contin: ;}do {/* ... */continue;/* ... */contin: ;} while (/* ... */);for (/* ... */) {/* ... */continue;/* ... */contin: ;}unless the continue statement shown is in an enclosed iteration statement (in whichcase it is interpreted within that statement), it is equivalent to goto contin;.138)6.8.6.3 The break statementConstraints1A break statement shall appear only in or as a switch body or loop body.Semantics2A break statement terminates execution of the smallest enclosing switch or iterationstatement.138) Following the contin: label is a null statement.138Language§6.8.6.3WG14/N1256Committee Draft — Septermber 7, 2007ISO/IEC 9899:TC36.8.6.4 The return statementConstraints1A return statement with an expression shall not appear in a function whose return typeis void.
A return statement without an expression shall only appear in a functionwhose return type is void.Semantics2A return statement terminates execution of the current function and returns control toits caller. A function may have any number of return statements.3If a return statement with an expression is executed, the value of the expression isreturned to the caller as the value of the function call expression. If the expression has atype different from the return type of the function in which it appears, the value isconverted as if by assignment to an object having the return type of the function.139)4EXAMPLEIn:struct s { double i; } f(void);union {struct {int f1;struct s f2;} u1;struct {struct s f3;int f4;} u2;} g;struct s f(void){return g.u1.f2;}/* ... */g.u2.f3 = f();there is no undefined behavior, although there would be if the assignment were done directly (without usinga function call to fetch the value).139) The return statement is not an assignment.
The overlap restriction of subclause 6.5.16.1 does notapply to the case of function return. The representation of floating-point values may have wider rangeor precision and is determined by FLT_EVAL_METHOD. A cast may be used to remove this extrarange and precision.§6.8.6.4Language139ISO/IEC 9899:TC3Committee Draft — Septermber 7, 2007WG14/N12566.9 External definitionsSyntax1translation-unit:external-declarationtranslation-unit external-declarationexternal-declaration:function-definitiondeclarationConstraints2The storage-class specifiers auto and register shall not appear in the declarationspecifiers in an external declaration.3There shall be no more than one external definition for each identifier declared withinternal linkage in a translation unit.