Thread View: comp.lang.c
1 messages
1 total messages
Started by Andrey Tarasevich
Wed, 07 Oct 2026 03:36
Re: Taming The Switch-Statement
Author: Andrey Tarasevich
Date: Wed, 07 Oct 2026 03:36
Date: Wed, 07 Oct 2026 03:36
67 lines
3062 bytes
3062 bytes
On Sat 10/3/2026 9:36 AM, bart wrote:
> On 03/10/2026 17:02, David Brown wrote:
>> On 03/10/2026 10:43, Lawrence D’Oliveiro wrote:
>>> How not to forget to end each case-sequence in a switch statement with
>>> a break-statement: put the break at the same indentation level as the
>>> case-clause as well as the switch-expression. That way, you can scan
>>> your eye down a single line for each switch-statement to be sure that
>>> no breaks are missing. A reasonably-complex example, with a nested
>>> switch for added interest:
>>>
>>
>> gcc -Werror=implict-fallthrough
>
>
> I assume this is for non-empty blocks? Otherwise this will be an error:
>
> case 'A': case 'B': case 'C':
> // deal with ABC
This is not, but in other cases there are still some differences between
implementations.
For example, Clang approaches this matter rather formally. This:
switch (0)
{
case 0:;
case 1:;
case 2:;
}
is already sufficient to trigger fallthrough warnings in Clang under
`-Wimplicit-fallthrough`, even tough all "blocks" in the above are empty
for all practical means and purposes.
GCC will remain silent for the above code. This will keep it silent as well:
switch (0)
{
case 0:;
case 1:;
case 2: printf("World");
}
This produces no warnings either:
switch (0)
{
case 0: printf("Hello ");
case 1:;
case 2:;
}
And finally this:
switch (0)
{
case 0: printf("Hello ");
case 1:;
case 2: printf("World");
}
will trigger a warning under `-Wimplicit-fallthrough`. So, GCC appears
to look for a fallthrough from a non-empty to a non-empty before it warns.
--
Best regards,
Andrey
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