C89/C90: Returning 0 from main()

M

Michal Nazarewicz

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?
 
M

matevzb

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.
 
C

Christopher Benson-Manica

Michal Nazarewicz said:
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

Christopher Benson-Manica

Christopher Benson-Manica said:
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.
 
R

Robert Gamble

Michal said:
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
 
M

Michal Nazarewicz

matevzb said:
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.
 
P

Peter Nilsson

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.
 
C

Christopher Benson-Manica

Peter Nilsson said:
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.
 
P

Peter Nilsson

Christopher Benson-Manica said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top