strange case of void main

R

Rakesh Kumar

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

Ziggy

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);
}
 
E

Enrico `Trippo' Porreca

Ziggy said:
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.
 
R

Régis Troadec

"Ziggy" <[email protected]> a écrit dans le message de

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
 
G

Guillaume

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

Christian Bau

Rakesh Kumar said:
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.

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

Dan Pop

In said:
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
 
Z

Ziggy

Dan said:
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
 

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top