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.
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
main()
{
}
? I mean main() without declaration of a return value.
Thanks a lot,
Ioannis
> 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.
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
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.
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
>
"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.
>
>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'...
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.
> 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
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