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


Groups > comp.lang.c > #105761

Re: Return statement: anything goes?

From Noob <root@127.0.0.1>
Newsgroups comp.lang.c
Subject Re: Return statement: anything goes?
Date 2017-03-10 22:06 +0100
Organization A noiseless patient Spider
Message-ID <o9v4bu$u23$1@dont-email.me> (permalink)
References <o9utno$5tn$1@dont-email.me>

Show all headers | View raw


On 10/03/2017 20:13, BartC wrote:

> 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:
> 
>    int V;
> 
>    void A1(int x) { }
>    void A2(int x) { return;}
>    void A3(int x) { return 123;}
> 
>    int B1(int x) { }
>    int B2(int x) { return;}
>    int B3(int x) { return 123;}
>    int B4(int x) { return &V;}
>    int B5(int x) { if (1) return x;}
> 
>    void* C1(int x) { }
>    void* C2(int x) { return;}
>    void* C3(int x) { return 123;}
>    void* C4(int x) { return &V;}
> 
>    int main(void) { }                // M1
>    int main(void) { return;}         // M2
>    int main(void) { return 123;}     // M3
>    int main(void) { return &V;}      // M4
> 
> (The main() variations were tested one at a time.)
> 
> A selection of Windows C compilers gave these errors and warnings with 
> default options (E = error, W = warning, dash = no error):
> 
>              A1 A2 A3  B1 B2 B3 B4 B5  C1 C2 C3 C4  M1 M2 M3 M4
> 
>   Pelles C   -  -  W   W  E  -  E  -   -  E  E  -   -  E  -  E
>   lccwin     -  -  W   W  W  -  E  W   W  W  E  -   -  W  -  W
>   DMC        -  -  -   -  E  -  E  -   -  -  E  -   -  -  -  E
>   gcc/tdm    -  -  W   -  W  -  W  -   -  W  W  -   -  W  -  W
>   Tiny C     -  -  -   -  -  -  W  -   -  -  W  -   -  -  -  W
>   MSVC'08    -  -  W   W  W  -  W  -   W  W  W  -   -  -  -  -
> 
>   (bartc     -  -  E   E  E  -  E  E   E  E  E  -   *  E  -  E)
> 
> The last row shows what I consider to be errors, where I wouldn't be 
> happy with a wrong or missing return statement making it into code that 
> is to be executed.
> 
> It does rather seem that compiling C code is something of a lucky dip! 
> Surely source code is either correct or it isn't? Or is part of the 
> problem an attempt to keep compatibility with lax code and lax compilers 
> of the past?

What version of gcc are you testing with?
You might want to try running your test with
gcc -O2 -std=c99 -Wall -Werror

You'd get errors for A3, B1, B2, B4, C1, C2, C3.
(I didn't test the M* cases.)

Why do you expect an error for B5?

> [* I assume special rules apply to main(), in that a missing return 
> statement, or return expression, is quietly replaced by 'return 0']

No need to assume. You can look it up in the standard.

http://port70.net/~nsz/c/c11/
http://port70.net/~nsz/c/c99/

Regards.

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