Path: ...!eternal-september.org!feeder2.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Bart Newsgroups: comp.lang.c Subject: Re: else ladders practice Date: Thu, 7 Nov 2024 15:08:34 +0000 Organization: A noiseless patient Spider Lines: 33 Message-ID: References: <3deb64c5b0ee344acd9fbaea1002baf7302c1e8f@i2pn2.org> <6724CFD2.4030607@grunge.pl> <2491a699388b5891a49ef960e1ad8bb689fdc2ed@i2pn2.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Thu, 07 Nov 2024 16:08:34 +0100 (CET) Injection-Info: dont-email.me; posting-host="ec686777c96cc376bc9818100d47f072"; logging-data="2845737"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18QRBBD/7nwICBacV5PsbV5" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:HY2bkwy5EPI6ohbn4ZSpEuXaCyY= Content-Language: en-GB In-Reply-To: Bytes: 2744 On 07/11/2024 12:23, Bart wrote: > On 06/11/2024 14:50, David Brown wrote: >> C gets it right here.  There is no need for a return type when there >> is no return > > There is no return for only half the function! A function with a return > type is a function that CAN return. If it can't ever return, then make > it a procedure. > > Take this function where N can never be zero; is this the right way to > write it in C: > >    int F(int N) { >        if (N==0) unreachable(); >        return abc/N;              // abc is a global with value 100 >    } > > If doesn't look right. If I compile it with gcc (using > __builtin_unreachable), and call F(0), then it crashes. So it doesn't do > much does it?! It looks like it needs 'else' here. If I put that in, then F(0) returns either 0 or 1, so it returns garbage, whether or not 'unreachable' is used in the branch. So I'm struggling to see the point of it. Is it just to quieten the 'reaches end of non-void function' warning when used before the final '}'? In any case, 'unreachable' is a misnomer. 'shouldnt_be_reachable' is more accurate.