Strange return codes from old but good C program

Will Parsons varro at nodomain.invalid
Sun May 17 19:45:23 UTC 2015


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



More information about the freebsd-questions mailing list