From owner-freebsd-questions@FreeBSD.ORG Sun May 17 19:45:23 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 350F84C7 for ; Sun, 17 May 2015 19:45:23 +0000 (UTC) Received: from plane.gmane.org (plane.gmane.org [80.91.229.3]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id DF7F2162B for ; Sun, 17 May 2015 19:45:22 +0000 (UTC) Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1Yu4Uw-00034t-UW for freebsd-questions@freebsd.org; Sun, 17 May 2015 21:45:19 +0200 Received: from c-76-28-97-53.hsd1.ct.comcast.net ([76.28.97.53]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 17 May 2015 21:45:18 +0200 Received: from gyliamos by c-76-28-97-53.hsd1.ct.comcast.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 17 May 2015 21:45:18 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: freebsd-questions@freebsd.org From: Will Parsons Subject: Re: Strange return codes from old but good C program Date: Sun, 17 May 2015 19:45:09 +0000 (UTC) Lines: 30 Message-ID: References: <20150517204503.V69409@sola.nimnet.asn.au> <20150517124223.GA82704@ozzmosis.com> <5558A2D0.8080207@hiwaay.net> <20150517171713.09b01ec4.freebsd@edvax.de> Reply-To: gyliamos@gmail.com X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: c-76-28-97-53.hsd1.ct.comcast.net User-Agent: slrn/1.0.2 (FreeBSD) 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: Sun, 17 May 2015 19:45:23 -0000 On Sunday, 17 May 2015 3:17 PM UTC, Polytropon wrote: > On Sun, 17 May 2015 09:23:03 -0453, William A. Mahaffey III wrote: >> I believe it's more than plausible, it's defined that unspecified return >> *will* return random garbage (more precisely, 'results are >> undefined').... not 100% certain, but about 99.9% confident. $0.02, no >> more, no less .... > > If I remember correctly, leaving main() - this is what > happens after the last statement executed - will call > the kernel function _exit which will then deal with > the return code, something that is _usually_ done using > a return 0; statement (or exit(), abort() and the like). > > main() itself is valid, because the compiler will relapse > to the default return type, which is (int), at least in > K&R per standard. So finding some implementation-specific > return code - or garbage, valid as well - will be the > result to be expected. > > Adding the common exit statement "return 0;" as the last > instruction will make sure the program will behave nicely > within shells and scripts that check for return codes. 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". -- Will