C89/C90: Returning 0 from main()

6 views
Skip to first unread message

Michal Nazarewicz

unread,
Feb 13, 2007, 7:57:37 AM2/13/07
to
Hi,

What does returning 0 from main() mean according to C89/C90 standard?
I've found that in C99 it means successful termination (7.20.4.3p5)
however as I'm editing book on C at Polish Wikibooks I'd like to know
what C89/C90 says about it - quotation from C89 or C90 would be nice.

Moreover, in C99 main() function is somewhat special because lack of
return statement is equivalent with returning zero (5.1.2.2.3p1). Is
it also the case in C89/C90?

BTW. Is there any publicly available (draft of) C89/C90 standard?

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--

matevzb

unread,
Feb 13, 2007, 8:21:34 AM2/13/07
to
On Feb 13, 1:57 pm, Michal Nazarewicz <min...@tlen.pl> wrote:
> Hi,
>
> What does returning 0 from main() mean according to C89/C90 standard?
> I've found that in C99 it means successful termination (7.20.4.3p5)
> however as I'm editing book on C at Polish Wikibooks I'd like to know
> what C89/C90 says about it - quotation from C89 or C90 would be nice.
First, the main() quote:
"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. If the main function executes a return that specifies
no value, the termination status returned to the host environment is
undefined."
Return statement:
"Reaching the } that terminates a function is equivalent to executing
a return statement without an expression."
exit() function:
"Finally, control is returned to the host environment. If the value of
status is zero or EXIT_SUCCESS , an implementation-defined form of the
status successful termination is returned. If the value of status is
EXIT_FAILURE , an implementation-defined form of the status
unsuccessful termination is returned. Otherwise the status returned is
implementation-defined."

> Moreover, in C99 main() function is somewhat special because lack of
> return statement is equivalent with returning zero (5.1.2.2.3p1). Is
> it also the case in C89/C90?
It doesn't seem to be with regards to the quoted material, but someone
else should confirm this.

> BTW. Is there any publicly available (draft of) C89/C90 standard?
It's available at http://web.archive.org/web/20050207005628/http://
dev.unicals.com/papers/c89-draft.html.
--
WYCIWYG - what you C is what you get

Christopher Benson-Manica

unread,
Feb 13, 2007, 8:25:43 AM2/13/07
to
Michal Nazarewicz <min...@tlen.pl> wrote:

> What does returning 0 from main() mean according to C89/C90 standard?

From n869 [1]:

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;"

7.20.4.3 (The exit function), subparagraph 5: "If the value of status
is zero or EXIT_SUCCESS, an implementation-defined form of the status
successful termination is returned."

> Moreover, in C99 main() function is somewhat special because lack of


> return statement is equivalent with returning zero (5.1.2.2.3p1). Is
> it also the case in C89/C90?

No; in C89, omission of the return statement causes an unspecified
termination status to be returned from the host environment (I will
leave quoting the relevant portion of the standard to someone with
more time to look it up).

> BTW. Is there any publicly available (draft of) C89/C90 standard?

[1] I apparently have been laboring under the delusion that n869 was a
draft of the C89 Standard; a look at the foreward of that document
reveals that it is not so. I also thought the FAQ addressed this
question, but I don't see that either. I've started my morning 0/2
and mostly failed to be helpful; sorry about that.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.

Christopher Benson-Manica

unread,
Feb 13, 2007, 8:27:28 AM2/13/07
to
Christopher Benson-Manica <at...@otaku.freeshell.org> wrote:

> No; in C89, omission of the return statement causes an unspecified
> termination status to be returned from the host environment (I will

D'oh - of course, that should be "TO the host environment". Perhaps I
should forego attempting to work today, or at least head back to
Starbucks.

Robert Gamble

unread,
Feb 13, 2007, 8:37:51 AM2/13/07
to
Michal Nazarewicz wrote:
> Hi,
>
> What does returning 0 from main() mean according to C89/C90 standard?
> I've found that in C99 it means successful termination (7.20.4.3p5)
> however as I'm editing book on C at Polish Wikibooks I'd like to know
> what C89/C90 says about it - quotation from C89 or C90 would be nice.

The same verbiage you referred to is found in C89 at 7.10.4.3p5.

> Moreover, in C99 main() function is somewhat special because lack of
> return statement is equivalent with returning zero (5.1.2.2.3p1). Is
> it also the case in C89/C90?

No, the value returned in this case is undefined.

> BTW. Is there any publicly available (draft of) C89/C90 standard?

I'm sure there is, I don't know where one would find it though.

Robert Gamble

Michal Nazarewicz

unread,
Feb 13, 2007, 10:26:16 AM2/13/07
to
> On Feb 13, 1:57 pm, Michal Nazarewicz <min...@tlen.pl> wrote:
>> What does returning 0 from main() mean according to C89/C90 standard?
>> I've found that in C99 it means successful termination (7.20.4.3p5)
>> however as I'm editing book on C at Polish Wikibooks I'd like to know
>> what C89/C90 says about it - quotation from C89 or C90 would be nice.

"matevzb" <mat...@gmail.com> writes:
> First, the main() quote:
> "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. If the main function executes a return that specifies
> no value, the termination status returned to the host environment is
> undefined."
> Return statement:
> "Reaching the } that terminates a function is equivalent to executing
> a return statement without an expression."
> exit() function:
> "Finally, control is returned to the host environment. If the value of
> status is zero or EXIT_SUCCESS , an implementation-defined form of the
> status successful termination is returned. If the value of status is
> EXIT_FAILURE , an implementation-defined form of the status
> unsuccessful termination is returned. Otherwise the status returned is
> implementation-defined."

Thanks, that answers all my questions.

Peter Nilsson

unread,
Feb 13, 2007, 5:20:59 PM2/13/07
to
Christopher Benson-Manica <a...@otaku.freeshell.org> wrote:
> Michal Nazarewicz <min...@tlen.pl> wrote:
> > What does returning 0 from main() mean according to C89/C90
> > standard?

C89 draft:

"Program termination"

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. If the main function


executes a return that specifies no value, the termination
status returned to the host environment is undefined.

> From n869 [1]:


>
> 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;"
>
> 7.20.4.3 (The exit function), subparagraph 5: "If the value of
> status is zero or EXIT_SUCCESS, an implementation-defined form
> of the status successful termination is returned."
>
> > Moreover, in C99 main() function is somewhat special because
> > lack of return statement is equivalent with returning zero
> > (5.1.2.2.3p1).

True.

> > Is it also the case in C89/C90?
>
> No; in C89, omission of the return statement causes an unspecified

> termination status to be returned [to] the host environment

It does so for the _initial_ call to main. Recursive calls to main
are allowed, and it's possible to perform such calls legitimately
without main ever returning a value, so long as the calling function
doesn't attempt to use the return value.

> (I will
> leave quoting the relevant portion of the standard to someone with
> more time to look it up).

Strictly speaking it says an _undefined_ status will be returned to
the host. Sloppy wording perhaps, but it's probably meant to convey
that the C standard doesn't limit the termination status to something
as simple as an integer value or C like object. For instance, an
unsuccessful status might be to ask the host to ring the lab klaxons!

> > BTW. Is there any publicly available (draft of) C89/C90 standard?
>
> [1] I apparently have been laboring under the delusion that n869
> was a draft of the C89 Standard; a look at the foreward of that
> document reveals that it is not so. I also thought the FAQ
> addressed this question, but I don't see that either. I've started
> my morning 0/2 and mostly failed to be helpful; sorry about that.

N869 is the last public draft prior to the issuance of C99. N1124 is
a more recent draft that includes the TCs.

--
Peter

Christopher Benson-Manica

unread,
Feb 13, 2007, 11:46:48 PM2/13/07
to
Peter Nilsson <ai...@acay.com.au> wrote:

> N869 is the last public draft prior to the issuance of C99. N1124 is
> a more recent draft that includes the TCs.

However, N869 describes a language that has much more in common with
C99 than C89, correct? That's the fact that I've previously missed.

Peter Nilsson

unread,
Feb 14, 2007, 5:23:53 PM2/14/07
to
Christopher Benson-Manica <a...@otaku.freeshell.org> wrote:

> Peter Nilsson <a...@acay.com.au> wrote:
> > N869 is the last public draft prior to the issuance of C99.
> > N1124 is a more recent draft that includes the TCs.
>
> However, N869 describes a language that has much more in common
> with C99 than C89, correct? That's the fact that I've previously
> missed.

It's a draft of C99 yes, but in terms of being a reference for
C89 it's quite useful. N1124 is better still. C99 merely clarified
and extended C89 for the most part. The changes are summarised
in the Forward.

There are differences of course, but C99 reflects the modern
spirit of C better than the crusty C89 standard.

--
Peter

Reply all
Reply to author
Forward
0 new messages