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


Groups > comp.lang.c > #105749

Re: Return statement: anything goes?

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Return statement: anything goes?
Date 2017-03-10 12:27 -0800
Organization None to speak of
Message-ID <lna88sho76.fsf@kst-u.example.com> (permalink)
References <o9utno$5tn$1@dont-email.me>

Show all headers | View raw


BartC <bc@freeuk.com> writes:
> This is something that came up last week, and was another example of C 
> compilers all reacting differently to the same errors.
>
> In this case, whether or not a return statement is present, and whether 
> it returns the correct type of expression:

See N1570 sections 6.9.1 (Function definitions) and 6.8.6.4 (The return
statement).

>    int V;
>
>    void A1(int x) { }
Valid.
>    void A2(int x) { return;}
Valid.
>    void A3(int x) { return 123;}
Constraint violation.

>    int B1(int x) { }
Valid, UB if the caller tries to use the result.
>    int B2(int x) { return;}
C90: Valid, UB if the caller tries to use the result.
C99 and later: Constraint violation.
>    int B3(int x) { return 123;}
Valid.
>    int B4(int x) { return &V;}
Constraint violation.
>    int B5(int x) { if (1) return x;}
Valid.

>    void* C1(int x) { }
Valid, UB if the caller tries to use the result.
>    void* C2(int x) { return;}
C90: Valid, UB if the caller tries to use the result.
C99 and later: Constraint violation.
>    void* C3(int x) { return 123;}
Constraint violation.
>    void* C4(int x) { return &V;}
Valid, the int* value is implicitly converted to void*.

>    int main(void) { }                // M1
C90: Valid, returns an undefined status to the environment.
C99 and later: Valid, does an implicit "return 0;" as of C99.
>    int main(void) { return;}         // M2
C90: Valid, returns an undefined status to the environment.
C99 and later: Constraint violation.
>    int main(void) { return 123;}     // M3
Valid.  (The value 123 might not be meaningful.)
>    int main(void) { return &V;}      // M4
Constraint violation.

I've tried to make this accurate, but I can't guarantee I've succeeded.

I'll leave it to you to decide which compilers are conforming or not,
and to which edition(s) of the standard.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Next 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