Thread View: comp.lang.c
1 messages
1 total messages
Started by Tim Rentsch
Fri, 16 Oct 2026 11:46
Re: Taming The Switch-Statement
Author: Tim Rentsch
Date: Fri, 16 Oct 2026 11:46
Date: Fri, 16 Oct 2026 11:46
60 lines
3475 bytes
3475 bytes
Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
> [.. some syntanx changes were made as part of C23 ..]
>
> C23 made some changes with the goal of allowing a label to be
> immediately followed by a declaration or `}`. It did so by changing
> the grammar for a compound-statement:
>
> compound-statement:
> { block-item-list[opt] }
>
> block-item-list:
> block-item
> block-item-list block-item
>
> block-item:
> declaration
> unlabeled-statement
> label
>
> This means that in
>
> switch (x) {
> case 'A':
> case 'B':
> case 'C':
> puts("blah");
> }
>
> *there is no labeled-statement*. Instead, each of `case 'A':`,
> `case 'B':`, `case 'C':`, and `puts("blah");` is a block-item, and all
> the block-items are at the same syntactic level within the
> compound-statement. (The indentation is still appropriate IMHO.)
>
> Since control jumps to "the statement or declaration following the
> matched case label", there is no fallthrough. If x == 'A', control
> jumps directly to the puts() call.
>
> In C17 and earlier, the same wording implies that when x=='A', control
> jumps to the statement
> case 'B': case 'C': puts("blah");
> so one might arguably refer to that as "fallthrough". In C23,
> case 'B': case 'C': puts("blah");
> is not a statement.
>
> A weird but mostly harmless consequence of this is that C23 keeps
> the syntactic construct of a *labeled-statement*, but it's used
> extremely rarely. Any C17-or-earlier labeled-statement at the
> top level of an enclosing compound-statement is no longer a C23
> labeled-statement, even though it looks like one. In C23, a
> labeled-statement can only occur in an odd construct like this:
>
> if (condition)
> LABEL: statement;
>
> The proposal that led to this suggested an optional additional change,
> making labels outside of compound statements an obsolescent feature.
> That part of the proposal was not adopted.
Glad to hear it. Good choice.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads