Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #105854

Re: Return statement: anything goes?

From BartC <bc@freeuk.com>
Newsgroups comp.lang.c
Subject Re: Return statement: anything goes?
Date 2017-03-12 12:51 +0000
Organization A noiseless patient Spider
Message-ID <oa3g3s$eda$1@dont-email.me> (permalink)
References (1 earlier) <o9v4bu$u23$1@dont-email.me> <e657889d-45ad-402b-a962-a6948ed3ec7c@googlegroups.com> <oa2mc0$eo8$1@z-news.wcss.wroc.pl> <oa393j$p0e$1@dont-email.me> <87innewuuq.fsf@bsb.me.uk>

Show all headers | View raw


On 12/03/2017 12:19, Ben Bacarisse wrote:
> BartC <bc@freeuk.com> writes:
>
>> On 12/03/2017 05:29, antispam@math.uni.wroc.pl wrote:
>>> bart4858@gmail.com wrote:
>>>> On Friday, 10 March 2017 21:06:54 UTC, Noob  wrote:
>>>>
>>>>> Why do you expect an error for B5?
>>>>
>>>> I should have used 'if (x)' rather than 'if (1)'. I'll test again once I get my machine back (currently used for gaming).
>>>>
>>>> (My own code would test that an explicit 'return expr;' is encountered before running into the closing '}'. A closing 'if' would need both branches and each would need to end with 'return'.)
>>>>
>>>
>>> What about (assuming usual declaration of 'open' and 'exit'):
>>>
>>> int
>>> do_open(const char * fname)
>>> {
>>>    int fd = open(fname, O_RDONLY);
>>>    if (fd < 0) {
>>>        exit(1);
>>>    } else {
>>>        return fd;
>>>    }
>>> }
>>
>> I checked the equivalent in other language where I implement this
>> check, and that works (exit(1) is written 'stop 1', and 'stop' is
>> acceptable too).
>
> That might be to miss antispam's point.  exit happens to be known to the
> compiler but any function call it (or call a function that calls it...):
>
>   if (C)
>       print_error_and_exit("hey!);
>   else return 42;

This shows the advantage of being able to stipulate that a non-void 
function /has/ to end with an explicit statement, either directly, or in 
each branch of a conditional. (Loops won't qualify, and 'switch' is too 
disorganised to do a proper check on.)

In the case of your example, that could just mean adding a dummy return 
statement (something the compiler might be obliged to do anyway), or 
rewriting it as:

    if (C)
        print_error_and_exit("hey!);
    return 42;

But as it is, I may have to forget the idea of doing such a check if the 
language and existing coding practices won't cooperate.

> C11 has the _Noreturn function specifier.

I doubt that would help much as the vast majority of code won't be using 
it. Not even new code (who's going to bother? It would also limit 
portability to C11 only).


-- 
bartc

Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Return statement: anything goes? BartC <bc@freeuk.com> - 2017-03-10 19:13 +0000
  Re: Return statement: anything goes? John Bode <jfbode1029@gmail.com> - 2017-03-10 11:59 -0800
    Re: Return statement: anything goes? Keith Thompson <kst-u@mib.org> - 2017-03-10 12:34 -0800
  Re: Return statement: anything goes? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-10 20:17 +0000
    Re: Return statement: anything goes? bart4858@gmail.com - 2017-03-10 13:17 -0800
      Re: Return statement: anything goes? Ian Collins <ian-news@hotmail.com> - 2017-03-11 10:30 +1300
      Re: Return statement: anything goes? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-11 01:25 +0000
        Re: Return statement: anything goes? BartC <bc@freeuk.com> - 2017-03-11 12:11 +0000
          Re: Return statement: anything goes? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-11 14:57 +0000
            Re: Return statement: anything goes? BartC <bc@freeuk.com> - 2017-03-11 16:31 +0000
              Re: Return statement: anything goes? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-11 17:47 +0000
                Re: Return statement: anything goes? BartC <bc@freeuk.com> - 2017-03-11 17:57 +0000
                Re: Return statement: anything goes? jameskuyper@verizon.net - 2017-03-11 10:59 -0800
                Re: Return statement: anything goes? BartC <bc@freeuk.com> - 2017-03-11 19:34 +0000
                Re: Return statement: anything goes? jameskuyper@verizon.net - 2017-03-11 11:59 -0800
                Re: Return statement: anything goes? supercat@casperkitty.com - 2017-03-11 12:26 -0800
                Re: Return statement: anything goes? supercat@casperkitty.com - 2017-03-11 12:22 -0800
                Re: Return statement: anything goes? jameskuyper@verizon.net - 2017-03-11 12:42 -0800
                Re: Return statement: anything goes? supercat@casperkitty.com - 2017-03-11 13:01 -0800
                Re: Return statement: anything goes? Gordon Burditt <gordon@hammy.burditt.org> - 2017-03-13 16:25 -0500
            Re: Return statement: anything goes? Gareth Owen <gwowen@gmail.com> - 2017-03-11 16:43 +0000
              Re: Return statement: anything goes? BartC <bc@freeuk.com> - 2017-03-11 18:13 +0000
                Re: Return statement: anything goes? BartC <bc@freeuk.com> - 2017-03-11 18:27 +0000
          Re: Return statement: anything goes? gordonb.rlpr3@burditt.org (Gordon Burditt) - 2017-03-13 16:18 -0500
        Re: Return statement: anything goes? supercat@casperkitty.com - 2017-03-11 12:44 -0800
      Re: Return statement: anything goes? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-03-11 07:43 -0800
        Re: Return statement: anything goes? BartC <bc@freeuk.com> - 2017-03-11 18:34 +0000
          Re: Return statement: anything goes? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-03-11 10:43 -0800
  Re: Return statement: anything goes? Keith Thompson <kst-u@mib.org> - 2017-03-10 12:27 -0800
  Re: Return statement: anything goes? Noob <root@127.0.0.1> - 2017-03-10 22:06 +0100
    Re: Return statement: anything goes? bart4858@gmail.com - 2017-03-10 13:26 -0800
      Re: Return statement: anything goes? antispam@math.uni.wroc.pl - 2017-03-12 05:29 +0000
        Re: Return statement: anything goes? BartC <bc@freeuk.com> - 2017-03-12 10:52 +0000
          Re: Return statement: anything goes? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-12 12:19 +0000
            Re: Return statement: anything goes? BartC <bc@freeuk.com> - 2017-03-12 12:51 +0000

csiph-web