void main()

Is it wrong to use void main()...instead of int main()
Isn't wrong as some compilers accept it but it isn't standard
Thanks!!!!!!!
Yes, it's wrong.
Why is it wrong????????????
From the C++ standards document:

3.6.1 Main function [basic.start.main]
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; startup
contains the execution of constructors for objects of namespace scope with static storage duration; termination
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. ]
3 The function main shall not be used (3.2) within a program. The linkage (3.5) of main is
implementation-defined. A program that declares main to be inline or static is ill-formed. The
name main is not otherwise reserved. [Example: member functions, classes, and enumerations can be
called main, as can entities in other namespaces. ]
Last edited on
The return value from main() can be used by the calling program.

In DOS/Windows shells, it can be accessed via the errorlevel. In Bourne Shell derivatives, it can be directly tested in a script.

If your implementation allowed you to get away with void main(), an unpredictable value will be returned instead. Really it shouldn't, so enforcement of int is correct.
Usually compilers if a return value is not given act as return 0; was used
If you mean when the function has a return type but not a return statement, not really. I believe VC++ returns a value filled with binary ones.
If you mean when a function has void as the return type, then the function simply doesn't return a value, and trying to assign nothing to a variable results in a compiler error.

The reason behind main() returning a value is completely irrelevant. The Standard explicitly defines that main() must return a value, and that's all anybody needs to know.
ahh ok now I understand..... tnx a lot guys....
Topic archived. No new replies allowed.