From owner-freebsd-questions@FreeBSD.ORG Tue May 19 11:04:59 2015 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 212FAA98 for ; Tue, 19 May 2015 11:04:59 +0000 (UTC) Received: from homiemail-a82.g.dreamhost.com (sub5.mail.dreamhost.com [208.113.200.129]) by mx1.freebsd.org (Postfix) with ESMTP id 01E801918 for ; Tue, 19 May 2015 11:04:58 +0000 (UTC) Received: from homiemail-a82.g.dreamhost.com (localhost [127.0.0.1]) by homiemail-a82.g.dreamhost.com (Postfix) with ESMTP id 67C7D282061; Tue, 19 May 2015 04:04:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=ozzmosis.com; h=date:from :to:cc:subject:message-id:references:mime-version:content-type :in-reply-to:content-transfer-encoding; s=ozzmosis.com; bh=qJzMm ygWZjlw1/to74n2d3/Q8WM=; b=kGXNPzeXmS0UfqzwOD4vmuCUlaiskHzVvskew 02uzzd3IQVAoNomeZXw2ToJW9kVCae4njKVARqJxGL4IKKu5l++2Lx3bP/ZCMI1X CR3MZYd1xH7m6wKqcmV+M2Lc3m/AZDBGn2ie4lhy7KT3CWx6TemXMGChJ5cBf5jY XjjXEA= Received: from blizzard.ozzmosis.com (203-206-127-199.dyn.iinet.net.au [203.206.127.199]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) (Authenticated sender: relay@ozzmosis.com) by homiemail-a82.g.dreamhost.com (Postfix) with ESMTPSA id EE90C28205F; Tue, 19 May 2015 04:04:57 -0700 (PDT) Received: by blizzard.ozzmosis.com (Postfix, from userid 1001) id 00682952; Tue, 19 May 2015 21:04:55 +1000 (AEST) Date: Tue, 19 May 2015 21:04:55 +1000 From: andrew clarke To: Polytropon Cc: Ian Smith , Trevor Roydhouse , gyliamos@gmail.com, freebsd-questions@freebsd.org Subject: Re: Strange return codes from old but good C program Message-ID: <20150519110455.GB88695@ozzmosis.com> References: <20150517204503.V69409@sola.nimnet.asn.au> <20150517124223.GA82704@ozzmosis.com> <5558A2D0.8080207@hiwaay.net> <20150517171713.09b01ec4.freebsd@edvax.de> <20150518135507.ada90d25.freebsd@edvax.de> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20150518135507.ada90d25.freebsd@edvax.de> User-Agent: Mutt/1.5.23 (2014-03-12) Content-Transfer-Encoding: quoted-printable X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 19 May 2015 11:04:59 -0000 On Mon 2015-05-18 13:55:07 UTC+0200, Polytropon (freebsd@edvax.de) wrote: > On Sun, 17 May 2015 19:45:09 +0000 (UTC), Will Parsons wrote: > > I don't have the actual C standard, but Harbison & Steele's "C - A > > Reference Manual" (which I think can be relied on) states "If the end > > of the body of *main* is reached without returning, it is treated as > > if *return 0;* were executed". After a bit of Googling, my understanding is that this is true for C99 (and probably C++98) but the earlier C89 (ANSI X3.159-1989) standard had no implicit return 0 for main(). See below for test results. Ian may want to recompile with -std=3Dc99 for main() to implicitly return 0. Or he could just add "return 0" at the end, probably. I haven't really looked at the code. $ rm -f *.o ssystem $ make -f unixl.mak CC=3Dgcc CFLAGS=3D-std=3Dc99 > In that case, no random return codes should appear. It also > doesn't meet the little test I've wrote (which again matches > with the initially described problem). >=20 > I have written (haha) the following "test case": >=20 > % cat returntest.c > main() { > int i, j; > for(i =3D 0, j =3D 0; i < 100; i++) > j +=3D i; > } >=20 > There are two "error" in it: main() doesn't have a return > type assigned, so per standard (int) will be assumed. And > there is no return statement. >=20 > Compiler is system's gcc (older system, obviously): > > % cc -Wall -o returntest returntest.c=20 > returntest.c:1: warning: return type defaults to 'int' > returntest.c: In function 'main': > returntest.c:5: warning: control reaches end of non-void function >=20 > This is what we expect. >=20 > But the program can be run, and we see: >=20 > % ./returntest ; echo $? > 99 Clang 3.4.1 on FreeBSD 10.1 amd64: $ clang -Wall -o returntest returntest.c returntest.c:1:1: warning: type specifier missing, defaults to 'int' [-Wi= mplicit-int] main() ^~~~ 1 warning generated. $ ./returntest ; echo $? 0 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D GNU C 4.8.4_3 on FreeBSD 10.1 amd64: $ gcc48 -Wall -o returntest returntest.c returntest.c:1:1: warning: return type defaults to 'int' [-Wreturn-type] main() ^ returntest.c: In function 'main': returntest.c:6:1: warning: control reaches end of non-void function [-Wre= turn-type] } ^ $ ./returntest ; echo $? 99 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D GNU C 4.8.4_3 on FreeBSD 10.1 amd64 using -std=3Dc99: $ gcc48 -std=3Dc99 -Wall -o returntest returntest.c returntest.c:1:1: warning: return type defaults to 'int' [enabled by defa= ult] main() ^ $ ./returntest ; echo $? 0 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D GNU C 4.8.2 on Ubuntu 14.04 x86_64: $ gcc -Wall -o returntest returntest.c returntest.c:1:1: warning: return type defaults to =E2=80=98int=E2=80=99 = [-Wreturn-type] main() ^ returntest.c: In function =E2=80=98main=E2=80=99: returntest.c:6:1: warning: control reaches end of non-void function [-Wre= turn-type] } ^ $ ./returntest ; echo $? 99 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D GNU C 4.8.2 on Ubuntu 14.04 x86_64 using -std=3Dc99: $ gcc -std=3Dc99 -Wall -o returntest returntest.c returntest.c:1:1: warning: return type defaults to =E2=80=98int=E2=80=99 = [enabled by default] main() ^ $ ./returntest ; echo $? 0 Regards Andrew