Home > Archive > C > April 2004 > Re: strange case of void main
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Re: strange case of void main
|
|
| Enrico `Trippo' Porreca 2004-04-22, 6:31 pm |
| Ziggy wrote:
> Just for curiosity.
> I know that main function is returning
> an int, but I came cross with the following
> code. (Unnessecary code is snipped) Is there
> any problem with it?
>
> #include <stdlib.h>
> void main(){
> exit(EXIT_SUCCESS);
> }
Yes: main *must* be declared as an int-returning function, even if you
call exit to terminate your program.
| |
| Guillaume 2004-04-22, 8:30 pm |
| > The only problem i see here is that ( as you have already put it), this
> does not conform to the latest spec. Try compiling on any of the latest
> compilers, and they won't accept this.
Well, even the latest gcc "only" gives a warning about this:
test1.c: In function `main':
test1.c:2: warning: return type of `main' is not `int'
| |
| Christian Bau 2004-04-23, 3:30 am |
| In article <40883c22$1@darkstar>,
Rakesh Kumar <dreamzlion_nospamplz@yahoo.com> wrote:
[color=darkred]
> The only problem i see here is that ( as you have already put it), this
> does not conform to the latest spec. Try compiling on any of the latest
> compilers, and they won't accept this.
>
> Ziggy wrote:
>
The biggest problem with the code is that it identifies the author as a
fool who either doesn't know the C language, or believes that standards
don't apply to him or her.
The problems can range from not compiling, which is really the best
thing that can happen, to returning a different status code than
expected, to crashing, to damaging things, and of course damaging the
authors reputation. All that is easily avoided by using int instead of
void.
| |
| Dan Pop 2004-04-23, 3:30 pm |
| In <c69dvc$3bj$1@nic.grnet.gr> Ziggy <d@d.net> writes:
>Just for curiosity.
>I know that main function is returning
>an int, but I came cross with the following
>code. (Unnessecary code is snipped) Is there
>any problem with it?
>
>#include <stdlib.h>
>void main(){
> exit(EXIT_SUCCESS);
>}
In C89 it's unconditional undefined behaviour. In C99 it's undefined
behaviour, unless the implementation *explicitly* defines this form of
main definition.
In the real life, if such code works, it works by accident, not by design.
The DEC C for VMS version I tried several years ago would not even
compile it.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de
| |
|
| Dan Pop wrote:
> In <c69dvc$3bj$1@nic.grnet.gr> Ziggy <d@d.net> writes:
>
>
>
>
> In C89 it's unconditional undefined behaviour. In C99 it's undefined
> behaviour, unless the implementation *explicitly* defines this form of
> main definition.
>
> In the real life, if such code works, it works by accident, not by design.
> The DEC C for VMS version I tried several years ago would not even
> compile it.
>
> Dan
Thanks everybody who replied.
Finally it seems to be one kind of implementation-defined feature.
peace
--
zig - zag - ziggy
| |
| Rakesh Kumar 2004-04-23, 7:33 pm |
| The only problem i see here is that ( as you have already put it), this
does not conform to the latest spec. Try compiling on any of the latest
compilers, and they won't accept this.
Ziggy wrote:
> Just for curiosity.
> I know that main function is returning
> an int, but I came cross with the following
> code. (Unnessecary code is snipped) Is there
> any problem with it?
>
> #include <stdlib.h>
> void main(){
> exit(EXIT_SUCCESS);
> }
>
--
Rakesh Kumar
** Remove nospamplz from my email address for my real email **
| |
| Régis Troadec 2004-04-25, 11:32 am |
|
"Ziggy" <d@d.net> a écrit dans le message de
news:c69dvc$3bj$1@nic.grnet.gr...
Hi,
> Just for curiosity.
> I know that main function is returning
> an int, but I came cross with the following
> code. (Unnessecary code is snipped) Is there
> any problem with it?
> #include <stdlib.h>
> void main(){
> exit(EXIT_SUCCESS);
> }
It's an unspecified behaviour and an undefined behavior in an hosted
environment (annexes J.1 and J.2) with the current standard. Nevertheless,
the behavior can be defined by the implementation. I let you find a copy of
the standard (C99, ISO/IEC 9899:1999) to see the definitions and the
differences between these terms, especially concerning the behavior's topic.
Having a draft of C99, I would recommend to read carefully §5.1.2 (execution
environments)
At program startup:
void main() isn't of the recommend form: it shall be int main(void) or int
main(int argc, char *argv[]) , §5.1.2.2.1
....But it could be another form defined by the implementation, see annex
J.3.2
Let's come back to §5.1.2.2.3, now about program termination, in your case,
main doesn't return anything (the return type is incompatible with int), the
termination status of the program returned to the host environnement is
unspecified.
Finally, by looking at the exit() function in §7.20.4.3, if the status
(parameter) of exit is EXIT_SUCCESS or 0, an implemention-defined form of a
successful termination is returned.
All that to say, rather than digging in a compiler documentation to obtain a
*correct* program whose the behavior is well-defined thanks to
implementation specific features, the best way would probably here to follow
the standard :)
Regis
>
> --
> Jazz is not dead, it just smells funny.
> (frank zappa)
>
|
|
|
|
|