public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Zan Lynx <zlynx@acm.org>
To: gcc@gcc.gnu.org
Subject: Re: calling exit in response to an error shouldn't be an error...
Date: Sun, 09 Jun 2019 21:52:00 -0000	[thread overview]
Message-ID: <fc220ba3-8365-80f7-e32b-e94a059ee122@acm.org> (raw)
In-Reply-To: <5CFD786D.6010603@tlinx.org>

I am not a GCC developer, just a regular user of C. But I have some 
comments below:

On 6/9/2019 3:21 PM, L A Walsh wrote:
> If I have a function returning NULL on error (including EOF).
> 
> So the program calls exit if the function doesn't return
> a non-zero value (func() || exit(1)).
> 
> I have:
> 
> --/tmp/ex.c--
> 
> main() { char * buf[512];
>      while (gets(buf) || exit(0)) puts(buf);
> }
> 
> -- compile w/:
> gcc -fpermissive --no-warnings  -o /tmp/ex /tmp/ex.c
> 
> Got:
> /tmp/ex.c: In function ‘main’:
> /tmp/ex.c:2:22: error: void value not ignored as it ought to be
>    while (gets(buf) || exit(0) ) puts(buf);
>                        ^~~~~~~
> 
> I understand that the while is testing the value of exit
> "when it returns", but since it doesn't, why flag an error
> (if exit is part of C-standard?) but *especially*, why
> not a warning, like a type mismatch or such?i

There is a slight misunderstanding of operators here. This is not while 
testing the value of exit(0). This is the "||" operator. It needs a 
value on *both* sides of || so it can evaluate them both and return a 
boolean result. Your code has no value on the right-hand side so it's 
meaningless.

> 
> 
> But this:
> 
> --/tmp/ex2.c--
> 
> main(){ char * buf[512];
>    while (1) {
>      fgets(buf) || exit(1);
>      fputs(buf);
>    }
> }
> 
> ---compile + output:
>> gcc -fpermissive --no-warnings  -o /tmp/ex2 /tmp/ex2.c
> /tmp/ex2.c: In function ‘main’:
> /tmp/ex2.c:4:18: error: void value not ignored as it ought to be
>      fgets(buf) || exit(1);
>                    ^~~~~~~
> 
> Ultra confusing -- how is exit value used?  Isn't it thrown away?

Same problem here.

I haven't tested it but you might be able to make this work with a comma 
operator like "while (gets(buf) || (exit(0), 1)) puts(buf);"

Oh, and "gets" is no longer a function. It's not just deprecated. It's 
*gone*.

-- 
                 Knowledge is Power -- Power Corrupts
                         Study Hard -- Be Evil

  reply	other threads:[~2019-06-09 21:52 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-09 21:21 L A Walsh
2019-06-09 21:52 ` Zan Lynx [this message]
2019-06-10 21:54   ` L A Walsh
2019-06-10 22:00     ` Jonathan Wakely
2019-06-10 22:03   ` P.S. - " L A Walsh

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=fc220ba3-8365-80f7-e32b-e94a059ee122@acm.org \
    --to=zlynx@acm.org \
    --cc=gcc@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).