public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* calling exit in response to an error shouldn't be an error...
@ 2019-06-09 21:21 L A Walsh
  2019-06-09 21:52 ` Zan Lynx
  0 siblings, 1 reply; 5+ messages in thread
From: L A Walsh @ 2019-06-09 21:21 UTC (permalink / raw)
  To: gcc

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?


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?




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2019-06-10 22:03 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-06-09 21:21 calling exit in response to an error shouldn't be an error L A Walsh
2019-06-09 21:52 ` Zan Lynx
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

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).