Стандарт языка Си С99 TC (1113411), страница 29
Текст из файла (страница 29)
Precisely the same effect could havebeen achieved byint y[4][3] = {1, 3, 5, 2, 4, 6, 3, 5, 7};The initializer for y[0] does not begin with a left brace, so three items from the list are used. Likewise thenext three are taken successively for y[1] and y[2].27EXAMPLE 4The declarationint z[4][3] = {{ 1 }, { 2 }, { 3 }, { 4 }};initializes the first column of z as specified and initializes the rest with zeros.28EXAMPLE 5The declarationstruct { int a[3], b; } w[] = { { 1 }, 2 };is a definition with an inconsistently bracketed initialization.
It defines an array with two elementstructures: w[0].a[0] is 1 and w[1].a[0] is 2; all the other elements are zero.133) In particular, the evaluation order need not be the same as the order of subobject initialization.128Language§6.7.8WG14/N125629EXAMPLE 6Committee Draft — Septermber 7, 2007ISO/IEC 9899:TC3The declarationshort q[4][3][2] = {{ 1 },{ 2, 3 },{ 4, 5, 6 }};contains an incompletely but consistently bracketed initialization. It defines a three-dimensional arrayobject: q[0][0][0] is 1, q[1][0][0] is 2, q[1][0][1] is 3, and 4, 5, and 6 initializeq[2][0][0], q[2][0][1], and q[2][1][0], respectively; all the rest are zero.
The initializer forq[0][0] does not begin with a left brace, so up to six items from the current list may be used. There isonly one, so the values for the remaining five elements are initialized with zero. Likewise, the initializersfor q[1][0] and q[2][0] do not begin with a left brace, so each uses up to six items, initializing theirrespective two-dimensional subaggregates.
If there had been more than six items in any of the lists, adiagnostic message would have been issued. The same initialization result could have been achieved by:short q[4][3][2] = {1, 0, 0, 0, 0, 0,2, 3, 0, 0, 0, 0,4, 5, 6};or by:short q[4][3][2] = {{{ 1 },},{{ 2, 3 },},{{ 4, 5 },{ 6 },}};in a fully bracketed form.30Note that the fully bracketed and minimally bracketed forms of initialization are, in general, less likely tocause confusion.31EXAMPLE 7declarationOne form of initialization that completes array types involves typedef names.
Given thetypedef int A[];// OK - declared with block scopethe declarationA a = { 1, 2 }, b = { 3, 4, 5 };is identical toint a[] = { 1, 2 }, b[] = { 3, 4, 5 };due to the rules for incomplete types.§6.7.8Language129ISO/IEC 9899:TC332EXAMPLE 8Committee Draft — Septermber 7, 2007WG14/N1256The declarationchar s[] = "abc", t[3] = "abc";defines ‘‘plain’’ char array objects s and t whose elements are initialized with character string literals.This declaration is identical tochar s[] = { 'a', 'b', 'c', '\0' },t[] = { 'a', 'b', 'c' };The contents of the arrays are modifiable. On the other hand, the declarationchar *p = "abc";defines p with type ‘‘pointer to char’’ and initializes it to point to an object with type ‘‘array of char’’with length 4 whose elements are initialized with a character string literal.
If an attempt is made to use p tomodify the contents of the array, the behavior is undefined.33EXAMPLE 9designators:Arrays can be initialized to correspond to the elements of an enumeration by usingenum { member_one,const char *nm[] =[member_two][member_one]};34EXAMPLE 10member_two };{= "member two",= "member one",Structure members can be initialized to nonzero values without depending on their order:div_t answer = { .quot = 2, .rem = -1 };35EXAMPLE 11 Designators can be used to provide explicit initialization when unadorned initializer listsmight be misunderstood:struct { int a[3], b; } w[] ={ [0].a = {1}, [1].a[0] = 2 };36EXAMPLE 12Space can be ‘‘allocated’’ from both ends of an array by using a single designator:int a[MAX] = {1, 3, 5, 7, 9, [MAX-5] = 8, 6, 4, 2, 0};37In the above, if MAX is greater than ten, there will be some zero-valued elements in the middle; if it is lessthan ten, some of the values provided by the first five initializers will be overridden by the second five.38EXAMPLE 13Any member of a union can be initialized:union { /* ...
*/ } u = { .any_member = 42 };Forward references: common definitions <stddef.h> (7.17).130Language§6.7.8WG14/N1256Committee Draft — Septermber 7, 2007ISO/IEC 9899:TC36.8 Statements and blocksSyntax1statement:labeled-statementcompound-statementexpression-statementselection-statementiteration-statementjump-statementSemantics2A statement specifies an action to be performed. Except as indicated, statements areexecuted in sequence.3A block allows a set of declarations and statements to be grouped into one syntactic unit.The initializers of objects that have automatic storage duration, and the variable lengtharray declarators of ordinary identifiers with block scope, are evaluated and the values arestored in the objects (including storing an indeterminate value in objects without aninitializer) each time the declaration is reached in the order of execution, as if it were astatement, and within each declaration in the order that declarators appear.4A full expression is an expression that is not part of another expression or of a declarator.Each of the following is a full expression: an initializer; the expression in an expressionstatement; the controlling expression of a selection statement (if or switch); thecontrolling expression of a while or do statement; each of the (optional) expressions ofa for statement; the (optional) expression in a return statement.
The end of a fullexpression is a sequence point.Forward references: expression and null statements (6.8.3), selection statements(6.8.4), iteration statements (6.8.5), the return statement (6.8.6.4).6.8.1 Labeled statementsSyntax1labeled-statement:identifier : statementcase constant-expression : statementdefault : statementConstraints2A case or default label shall appear only in a switch statement.
Furtherconstraints on such labels are discussed under the switch statement.§6.8.1Language131ISO/IEC 9899:TC33Committee Draft — Septermber 7, 2007WG14/N1256Label names shall be unique within a function.Semantics4Any statement may be preceded by a prefix that declares an identifier as a label name.Labels in themselves do not alter the flow of control, which continues unimpeded acrossthem.Forward references: the goto statement (6.8.6.1), the switch statement (6.8.4.2).6.8.2 Compound statementSyntax1compound-statement:{ block-item-listopt }block-item-list:block-itemblock-item-list block-itemblock-item:declarationstatementSemantics2A compound statement is a block.6.8.3 Expression and null statementsSyntax1expression-statement:expressionopt ;Semantics2The expression in an expression statement is evaluated as a void expression for its sideeffects.134)3A null statement (consisting of just a semicolon) performs no operations.4EXAMPLE 1 If a function call is evaluated as an expression statement for its side effects only, thediscarding of its value may be made explicit by converting the expression to a void expression by means ofa cast:int p(int);/* ...
*/(void)p(0);134) Such as assignments, and function calls which have side effects.132Language§6.8.3WG14/N12565EXAMPLE 2Committee Draft — Septermber 7, 2007ISO/IEC 9899:TC3In the program fragmentchar *s;/* ... */while (*s++ != '\0');a null statement is used to supply an empty loop body to the iteration statement.6EXAMPLE 3statement.A null statement may also be used to carry a label just before the closing } of a compoundwhile (loop1) {/* ...
*/while (loop2) {/* ... */if (want_out)goto end_loop1;/* ... */}/* ... */end_loop1: ;}Forward references: iteration statements (6.8.5).6.8.4 Selection statementsSyntax1selection-statement:if ( expression ) statementif ( expression ) statement else statementswitch ( expression ) statementSemantics2A selection statement selects among a set of statements depending on the value of acontrolling expression.3A selection statement is a block whose scope is a strict subset of the scope of itsenclosing block. Each associated substatement is also a block whose scope is a strictsubset of the scope of the selection statement.6.8.4.1 The if statementConstraints1The controlling expression of an if statement shall have scalar type.Semantics2In both forms, the first substatement is executed if the expression compares unequal to 0.In the else form, the second substatement is executed if the expression compares equal§6.8.4.1Language133ISO/IEC 9899:TC3Committee Draft — Septermber 7, 2007WG14/N1256to 0.
If the first substatement is reached via a label, the second substatement is notexecuted.3An else is associated with the lexically nearest preceding if that is allowed by thesyntax.6.8.4.2 The switch statementConstraints1The controlling expression of a switch statement shall have integer type.2If a switch statement has an associated case or default label within the scope of anidentifier with a variably modified type, the entire switch statement shall be within thescope of that identifier.135)3The expression of each case label shall be an integer constant expression and no two ofthe case constant expressions in the same switch statement shall have the same valueafter conversion.
There may be at most one default label in a switch statement.(Any enclosed switch statement may have a default label or case constantexpressions with values that duplicate case constant expressions in the enclosingswitch statement.)Semantics4A switch statement causes control to jump to, into, or past the statement that is theswitch body, depending on the value of a controlling expression, and on the presence of adefault label and the values of any case labels on or in the switch body.