From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 23346 invoked by alias); 10 Jun 2019 22:00:34 -0000 Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org Received: (qmail 23224 invoked by uid 89); 10 Jun 2019 22:00:24 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-4.3 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,GIT_PATCH_2,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=ham version=3.3.1 spammy=H*i:sk:5CFED18, H*f:sk:5CFED18 X-HELO: mail-wr1-f45.google.com Received: from mail-wr1-f45.google.com (HELO mail-wr1-f45.google.com) (209.85.221.45) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 10 Jun 2019 22:00:23 +0000 Received: by mail-wr1-f45.google.com with SMTP id p11so10722753wre.7 for ; Mon, 10 Jun 2019 15:00:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=n7ay0NUEOIjJxQH/wYvjfxUgvdwzaqzABMcbc66i6o4=; b=oL53dwOwba1rN9Nqc6FqJMWbcH76jYJAGxntvMplU70lyVaA9315WwRghamzVTqOos tIPCNCWNtg2N2pNb0pzf+quz0IX+HroILPAVjhFJq0UFutCvOKAPayeTuWG/nE4MwdEl dYLfQteSsXrh+A+F6fL3r1THmVyy7Ta4YhtBrWDyc3tXS7rAOxyR/JqTWWj39Bpm+XKc mcmBfJxEjd5SAYzbDNLhdiucMCFQq8qHzLxarQJYEUdw1LgG6wdJ30LHBTr/lByVULfk EZQ7R4klp1228WWCxfM54OqdJ7zjY/Cx0SJwQ6E7xBDuozMJdeKD/PZVLA740TlU25If fViA== MIME-Version: 1.0 References: <5CFD786D.6010603@tlinx.org> <5CFED18A.8040601@tlinx.org> In-Reply-To: <5CFED18A.8040601@tlinx.org> From: Jonathan Wakely Date: Mon, 10 Jun 2019 22:00:00 -0000 Message-ID: Subject: Re: calling exit in response to an error shouldn't be an error... To: L A Walsh Cc: Zan Lynx , "gcc@gcc.gnu.org" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-IsSubscribed: yes X-SW-Source: 2019-06/txt/msg00088.txt.bz2 On Mon, 10 Jun 2019 at 22:54, L A Walsh wrote: > > On 2019/06/09 14:52, Zan Lynx wrote: > > 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 =E2=80=98main=E2=80=99: > >> /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? > >> > > > > 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. > > > ---- > Um, but '||' and '&&' provide short circuiting. So in the case where That's irrelevant (but then this whole question is irrelevant to this mailing list and really belongs somewhere dedicated to C programming). > the first function is true, the 2nd won't be called. If the 2nd is called > it is guaranteed by the C11(17) standard not to return, so there can never > be a need for a return value in C11, as the program will terminate before > the value could be needed. But C is still a statically typed language. The || operator requires two operands with scalar type, so void is not allowed. The fact that exit doesn't return is not part of the type system. The return type of exit is void, and so you can't use it where void is not allowed. > Still works though. In gcc 8.2.1 (which is said to be C18 > compliant). gets is provide by your C library, not GCC.