Article View: comp.lang.c
Article #329Re: Taming The Switch-Statement
From: Johann 'Myrkraverk' Oskarsson
Date: Tue, 13 Oct 2026 12:04
Date: Tue, 13 Oct 2026 12:04
41 lines
3372 bytes
3372 bytes
On 10/13/2026 1:02 AM, Stefan Ram wrote:
> Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> wrote or quoted:
>> Not to me. A giant red flag would be something akin to
>> switch ( foo ) printf( "Hello, bart\n" ) ;
>> where the printf() never gets called.
>
> GCC also flags it:
>
> main.c: In function 'main':
> main.c:3:30: warning: statement will never be executed [-Wswitch-unreachable]
> 3 | int main( void ){ switch( 0 )printf( "Hello!\n" ); }
> | ^~~~~~~~~~~~~~~~~~~~
>
> . I use, " . . . -pedantic . . . -Wall -W -Wextra -Wpedantic . . .".
You're right. And a quick and immediate silence follows if we simply
transpose the printf() and 0, as follows.
signed main( void ){ switch( printf( "Hello!\n" ) ) break ; }
We could call this refactoring maneuver /switch castle/, after the cas-
tle movement in chess.
Now GCC does /not/ complain about a useless break, unlike bart [1].
But isn't that a bug in GCC? Shouldn't a useless break have the same
diagnostic as a bypassed printf()? Like an idiotic journalist, I ask
/inquiring minds want to know/?
Best wishes, and happy /switch castle/ refactoring adventures.
[1] Or was bart complaining that the break was /not/ useless, and com-
pulsory. I'm getting a bit confused trying to follow the argument, but
it's really popcorn worthy, and I guess I'll have to order industrial
amount of it next time this comes up.
--
Johann | email: invalid -> com | http://www.myrkraverk.com/blog/
I'm not from the Internet, I just work there. | via Easynews.com
https://bsky.app/profile/myrkraverk.bsky.social | for ( ;; ) _:;
Federated at https://fed.brid.gy/bsky/myrkraverk.bsky.social
Technical Details
Message-ID:
<M7pzS.141305$wun8.135843@fx13.ams4>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!peer01.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!fx13.ams4.POSTED!not-for-mailReferences:
<119qf6l$2rmg3$1@dont-email.me> <11ac0mi$19kfd$1@dont-email.me> <11ac2jv$1a84t$1@dont-email.me> <11ac783$1bkuo$1@kst.eternal-september.org> <11ad5eq$1l8hg$1@dont-email.me> <11aehqg$24k8m$3@kst.eternal-september.org> <11aenps$27sq7$1@dont-email.me> <11aetqv$29hmu$1@kst.eternal-september.org> <11afrfq$2j2o6$1@dont-email.me> <11agtkb$30q22$1@kst.eternal-september.org> <11ah16h$320sj$2@dont-email.me> <11ahqpu$38ir0$3@dont-email.me> <11aidi3$3gbu4$1@dont-email.me> <td6zS.60653$jNL6.31326@fx12.iad> <11aivdn$3nvmh$1@dont-email.me> <dI7zS.10223$AEj4.10181@fx17.ams4> <flag-20261012180101@ram.dialup.fu-berlin.de>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!peer01.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!fx13.ams4.POSTED!not-for-mail MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Taming The Switch-Statement Newsgroups: comp.lang.c References: <119qf6l$2rmg3$1@dont-email.me> <11ac0mi$19kfd$1@dont-email.me> <11ac2jv$1a84t$1@dont-email.me> <11ac783$1bkuo$1@kst.eternal-september.org> <11ad5eq$1l8hg$1@dont-email.me> <11aehqg$24k8m$3@kst.eternal-september.org> <11aenps$27sq7$1@dont-email.me> <11aetqv$29hmu$1@kst.eternal-september.org> <11afrfq$2j2o6$1@dont-email.me> <11agtkb$30q22$1@kst.eternal-september.org> <11ah16h$320sj$2@dont-email.me> <11ahqpu$38ir0$3@dont-email.me> <11aidi3$3gbu4$1@dont-email.me> <td6zS.60653$jNL6.31326@fx12.iad> <11aivdn$3nvmh$1@dont-email.me> <dI7zS.10223$AEj4.10181@fx17.ams4> <flag-20261012180101@ram.dialup.fu-berlin.de> Content-Language: en-US From: Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> Organization: Watcom Pro Ltd. In-Reply-To: <flag-20261012180101@ram.dialup.fu-berlin.de> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Lines: 41 Message-ID: <M7pzS.141305$wun8.135843@fx13.ams4> X-Complaints-To: abuse@easynews.com X-Complaints-Info: Please be sure to forward a copy of ALL headers otherwise we will be unable to process your complaint properly. Date: Tue, 13 Oct 2026 20:04:59 +0800 X-Received-Bytes: 3187