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

main() behavior in ANSI C++

0 views
Skip to first unread message

Keith Thompson

unread,
Aug 10, 2000, 3:00:00 AM8/10/00
to
"Ioannis Vranos" <not@available> writes:
> ANSI C++ permits a declaration of the type:
>
>
> main()
> {
> }
>
>
> ? I mean main() without declaration of a return value.

No.

In Andrew Koenig's article (you posted a followup to it without
reading the whole thing), he wrote:

] Subsequently, someone convinced the committee to require explicit
] return types on all functions. Although there was enough support
] for doing that, there wasn't enough support for removing the
] previously accepted compatibility hack.

Or you can reread any of the several (off-topic) articles recently
posted to comp.lang.c, several of which quoted directly from the C++
standard.

Note to comp.lang.c++ readers: there's been a flame war on this
subject in comp.lang.c, and it may now be leaking into comp.lang.c++.
Please remain calm.

--
Keith Thompson (The_Other_Keith) k...@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Welcome to the last year of the 20th century.

Victor Bazarov

unread,
Aug 10, 2000, 3:00:00 AM8/10/00
to
"Keith Thompson" <k...@cts.com> wrote...
> [...]

> Note to comp.lang.c++ readers: there's been a flame war on this
> subject in comp.lang.c, and it may now be leaking into comp.lang.c++.
> Please remain calm.

On the subject of main not having to have a return statement or
on subject of some people not reading other posts before replying?

Victor
--
Please remove capital A's from my address when replying by mail


Ioannis Vranos

unread,
Aug 11, 2000, 3:00:00 AM8/11/00
to
ANSI C++ permits a declaration of the type:


main()
{
}


? I mean main() without declaration of a return value.


Thanks a lot,
Ioannis


Martin Oberzalek

unread,
Aug 11, 2000, 3:00:00 AM8/11/00
to
"Ioannis Vranos" <not@available> writes:

> ANSI C++ permits a declaration of the type:
>
>
> main()
> {
> }
>
>
> ? I mean main() without declaration of a return value.

No, cause main is a C function, and C functions do not have a parameter
specific name.

In C you cannot overload functions.

--
The linux is with me.

Ioannis Vranos

unread,
Aug 11, 2000, 3:00:00 AM8/11/00
to
"Keith Thompson" <k...@cts.com> wrote in message
news:yecsnsc...@king.cts.com...

> Note to comp.lang.c++ readers: there's been a flame war on this
> subject in comp.lang.c, and it may now be leaking into comp.lang.c++.
> Please remain calm.

Relax, i just wanted to verify it in comp.lang.c++ too, because i have not
found any reference for that in bjarne stroustrup special edition 2nd
printing (march 2000), he uses only int main() (i use it myself too), and
where he talks about the default return of main() function (the programmer
not using a specific return sttement), he doesn't say if main() was
declared as int or not.

Can someone please post what exactly the Standard says about the
declaration of main() without a return type, if it prohibits it or defines
it as an undefined behavior?


Thanks a lot,
Ioannis

Dietmar Kuehl

unread,
Aug 11, 2000, 3:00:00 AM8/11/00
to
Hi,
In article <87u2csk...@hal.leo.at>,

Martin Oberzalek <kin...@gmx.at> wrote:
> "Ioannis Vranos" <not@available> writes:
> > ANSI C++ permits a declaration of the type:
> >
> > main()

No, it does not! In C++ to be portable you have to write one of the
following

int main()
int main(int, char*[])

with the second form optionally extended by given names to the
parameters. This is stated in section 3.6.1 of the C++ standard
(ISO/IEC FDIS 14882:1998(E)) paragraph 2:

An implementation shall not predefine the main function. This
function shall not be overloaded. It shall have a return type of type
int, but otherwise its type is implementation-defined. All
implementations shall allow both of the following definitions of
main:

int main() { /* ... */ }

and

int main(int argc, char* argv[]) { /* ... */ }

Since the "implicit in rule" is neither part of C++ (as defined by the
document mentioned above) nor part of C (as defined by ISO/IEC
9899/1999(E)) the given declaration of 'main()' has nothing to do with
C or C++.

If you want to discuss this issue, please give relevant quotes from the
mentioned documents!

> > {
> > }
> >
> >
> > ? I mean main() without declaration of a return value.

In C++ the ommission of the return *statement* (not the return type) is
explicitly allowed for 'main()' in section 3.6.1 paragraph 5
of the above mentioned document:

A return statement in main has the effect of leaving the main
function (destroying any objects with automatic storage duration)
and calling exit with the return value as the argument. If control
reaches the end of main without encountering a return
statement, the effect is that of executing


return 0;

This is a special rule applicable only to 'main()': It is undefined
behavior when control flows off the end of any other function non-void
function (with "flowing off the end" it is ment that the program
reaches the end of the function without a return statement).

> No, cause main is a C function, and C functions do not have a
> parameter specific name.

In C++, 'main()' is handled rather special. It is actually not even
considered to be a real function (which is a difference to C!). For
example, you cannot call 'main()' recursively, you cannot take the
address of 'main()'. It is definitely not viewed as a C function!

> In C you cannot overload functions.

Although you can overload functions in C++, you cannot overload
'main()'. However, you can choose portably between to alternatives how
to declare 'main()' (see above). A specific C++ implementation might
allow other forms of 'main()' (the version taking a third parameter,
namely a pointer to the environment comes to mind) but this is not
portable. In any case, *all* supported version of 'main()' have to
return 'int'. If this requirement is not met, a diagnostic is required.
--
<mailto:dietma...@yahoo.com>
<http://www.dietmar-kuehl.de/>


Sent via Deja.com http://www.deja.com/
Before you buy.

Rob Sykes

unread,
Aug 11, 2000, 3:00:00 AM8/11/00
to
"Ioannis Vranos" <not@available> wrote in message
news:3993e...@news2.prserv.net...

From the standard, para 3.6.1

1.) A program shall contain a global function called main, which is the
designated start of the program. It is
implementation-defined whether a program in a freestanding environment is
required to define a main
function. [Note: in a freestanding environment, start-up and termination is
implementation-defined; start-up
contains the execution of constructors for objects of namespace scope with
static storage duration; termi-nation
contains the execution of destructors for objects with static storage
duration. ]


2 An implementation shall not predefine the main function. This function
shall not be overloaded. It shall
have a return type of type int, but otherwise its type is
implementation-defined. All implementations
shall allow both of the following definitions of main:

int main() { /* ... */ }
and

int main(int argc, char* argv[]) { /* ... */ }

In the latter form argc shall be the number of arguments passed to the
program from the environment in
which the program is run. If argc is nonzero these arguments shall be
supplied in argv[0] through
argv[argc-1] as pointers to the initial characters of null-terminated
multibyte strings (NTMBSs)
(17.3.2.1.3.2) and argv[0] shall be the pointer to the initial character of
a NTMBS that represents the
name used to invoke the program or "". The value of argc shall be
nonnegative. The value of
argv[argc] shall be 0. [Note: it is recommended that any further (optional)
parameters be added after
argv. ]

.
.
.

Read well and wisely :-)

--
Rob Sykes

Remove NOSPAM to e-mail
>


Jim Hyslop

unread,
Aug 11, 2000, 3:00:00 AM8/11/00
to
In article <8n0q2u$bdt$1...@odin.john-richard.co.uk>,

"Rob Sykes" <r...@NOSPAMjohn-richard.co.uk> wrote:
> "Ioannis Vranos" <not@available> wrote in message
> news:3993e...@news2.prserv.net...
[snip]

> > Can someone please post what exactly the Standard says about the
> > declaration of main() without a return type, if it prohibits it or
> > defines
> > it as an undefined behavior?
[snip]

>
> From the standard, para 3.6.1
[snip quote from Standard]
In addition, don't forget the removal of "implicit int" as outlined in
7.1.5/2:

"At least one type-specifier that is not a cv-qualifier is required in a
declaration unless it declares a constructor, destructor or conversion
function. [Footnote 80]."

Footnote 80:
"... The 'implicit int' rule of C is no longer supported."

The declaration

main()
{
}

is, therefore, ill-formed because it has no type specifier.

It is not undefined behaviour, it is syntactically incorrect and a
conforming compiler should at the very least issue a diagnostic message.

--
Jim
I ignore all email from recruitment agencies. Except that
I reserve the right to send rude, nasty replies to recruiters.
Please do not send me email with questions - post
here.

Andre Kostur

unread,
Aug 11, 2000, 3:00:00 AM8/11/00
to
dietma...@yahoo.com (Dietmar Kuehl) wrote in
<8n0pc5$d2i$1...@nnrp1.deja.com>:

>
>In C++, 'main()' is handled rather special. It is actually not even
>considered to be a real function (which is a difference to C!). For
>example, you cannot call 'main()' recursively, you cannot take the
>address of 'main()'. It is definitely not viewed as a C function!
>

Whoa! Where does it state that in the standard? It's been a real long
time since I've had to ever recursively call main(), but I've never heard
it not being a 'function'...


Ron Natalie

unread,
Aug 11, 2000, 3:00:00 AM8/11/00
to

3.6.1 Main Function

3 The function main shall not be used in a program. The linkage is
implemenation-defined. A program that declares main as inline or
static is ill-formed.

Andre Kostur

unread,
Aug 11, 2000, 3:00:00 AM8/11/00
to
r...@sensor.com (Ron Natalie) wrote in <399428A0...@sensor.com>:

Ok... I lose :)

Jim Fischer

unread,
Aug 13, 2000, 3:00:00 AM8/13/00
to

"Ioannis Vranos" <not@available> wrote in message
news:39932...@news2.prserv.net...

> ANSI C++ permits a declaration of the type:
>
>
> main()

> {
> }
>
>
> ? I mean main() without declaration of a return value.

No.


1) ANSI/ISO C and C++ both require a return type of 'int' for function
main(). No other return type is allowed.

2) ANSI/ISO C and C++ do not support the notion of an "implicit int" data
type.

IOW, function main() in the code sample above does not have an explicit
return type of 'int' -- therefore the code is ill formed [IAW ANSI/ISO/IEC
14882:1998 and ANSI/ISO/IEC 9889:1999].


See also the last bullet item in:

http://www.cerfnet.com/~mpcline/C++-FAQs-Lite/how-to-post.html#[5.4]


Jim


Ron Natalie

unread,
Aug 14, 2000, 3:00:00 AM8/14/00
to

Jim Fischer wrote:

> IOW, function main() in the code sample above does not have an explicit
> return type of 'int' -- therefore the code is ill formed [IAW ANSI/ISO/IEC
> 14882:1998 and ANSI/ISO/IEC 9889:1999].
>

It's nice that you can read the standard, now read it well:

The C standard (9899:1999):

5.1.2.2.3 Program Termination

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. If the return type is not compatible with int, the
termination status returned to the host environment is unspecified.

The C++ standard (14882:1998):

3.6.1 Main function

0 new messages