ISO/IEC JTC1/SC22/WG14 N503 Proposal UK004a - Mix declarations and code Clive D.W. Feather Summary ------- C89 requires that all the declarations in a block preceed the first statement in that block. There is no particular reason for this restriction. This proposal allows arbitrary mixing of the two. Conformance ----------- No C89 strictly-conforming program is affected by this proposal. All programs that are newly strictly-conforming previously required a diagnostic because they violated the syntax of subclause 6.6.2. Discussion ---------- The only issue identified is that of jumping past declarations and automatic variable initializations in either direction. In C89, it is only possible to jump forward past one; it is now necessary to consider what happens with a backward jump. There are three options: (1) Forbid labels before the last declaration in the block. (2) Cause each initialization to be executed each time it is passed. (3) Make the results undefined. The first option, though superficially attractive, turns out to be overly complex when the issues of nested blocks are considered. The last seems to be too harsh; jumping around an initializer should be little different from having the block in a loop. Therefore the second choice has been used in this proposal. Detailed proposal ----------------- In 6.1.2.4, third paragraph, replace: If an initialization is specified for the value stored in the object, it is performed on each normal entry, but not if the block is entered by a jump to a labelled statement. with: If an initialization is specified for the value stored in the object, it is performed on each normal entry, but not if the block is entered by a jump to a labelled statement beyond the declaration. A backwards jump might cause the initializer to be evaluated more than once; if so, a new value will be stored each time. Replace subclause 6.6.2 syntax by: compound-statement: { block-item-list-opt } block-item-list: block-item: block-item-list block-item block-item: declaration statement Replace the last sentence of 6.6.2 semantics by: The initializers of objects that have automatic storage duration are evaluated, and the values stored in the objects (including storing an indeterminate value in objects without an initializer) each time that the declaration is reached in the order of execution, as if it were a statement, and within each declaration in the order that the declarators appear.