Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[JW] implicit "return 0;" guaranteed for recursive calls to main()?

34 views
Skip to first unread message

Jun Woong

unread,
Nov 19, 2002, 3:56:49 PM11/19/02
to
Hi.

C99 5.1.2.2.3:
] If the return type of the main function is a type compatible with
] int, a return from the initial call to the main function is
] equivalent to calling the exit function with the value returned by
] the main function as its argument; reaching the } that terminates
] the main function returns a value of 0.

Is the implicit "return 0;" guaranteed for all calls to main()? In
other words, can a recursive call to main() depend on the guarantee?
I think that the Standard guarantees it only for the initial call, but
not surely.

Thanks in advance.


--
Jun, Woong (myco...@hanmail.net)
Dept. of Physics, Univ. of Seoul


James Kuyper

unread,
Nov 19, 2002, 4:26:09 PM11/19/02
to
Jun Woong wrote:
>
> Hi.
>
> C99 5.1.2.2.3:
> ] If the return type of the main function is a type compatible with
> ] int, a return from the initial call to the main function is
> ] equivalent to calling the exit function with the value returned by
> ] the main function as its argument; reaching the } that terminates
> ] the main function returns a value of 0.
>
> Is the implicit "return 0;" guaranteed for all calls to main()? In
> other words, can a recursive call to main() depend on the guarantee?
> I think that the Standard guarantees it only for the initial call, but
> not surely.

I believe that the qualifier "from the initial call to main" applies
only to the clause that preceeds the ';'. The clause after the ';'
applies to all calls to main(), not just the initial call.

Clive D. W. Feather

unread,
Nov 26, 2002, 6:47:36 AM11/26/02
to
In article <are8ji$mfk$1...@tnews.hananet.net>, Jun Woong
<myco...@hanmail.net> writes

>C99 5.1.2.2.3:
>] If the return type of the main function is a type compatible with
>] int, a return from the initial call to the main function is
>] equivalent to calling the exit function with the value returned by
>] the main function as its argument; reaching the } that terminates
>] the main function returns a value of 0.
>
>Is the implicit "return 0;" guaranteed for all calls to main()?

No; that requirement is in a section headed "program termination" and
therefore only applies to the initial call to main.

All other calls are subject to the rule in 6.9.1:

[#12] If the } that terminates a function is reached, and
the value of the function call is used by the caller, the
behavior is undefined.

--
Clive D.W. Feather, writing for himself | Home: <cl...@davros.org>
Tel: +44 20 8371 1138 (work) | Web: <http://www.davros.org>
Fax: +44 870 051 9937 | Work: <cl...@demon.net>
Written on my laptop; please observe the Reply-To address

0 new messages