Possible Duplicate:
What is the difference between _tmain() and main() in C++?
How the void main(...), int main(..) and int _tmain() differs. They all are single entry and single exit systems. But in what situations we use these start-up functions?
Possible Duplicate:
What is the difference between _tmain() and main() in C++?
How the void main(...), int main(..) and int _tmain() differs. They all are single entry and single exit systems. But in what situations we use these start-up functions?
void main()
is invalid; the C++ standard requires main
to return int
. Some compilers let you get away with it.
int main()
is one of the two standard forms. The other is int main(int argc, char *argv[])
, which can be used to receive command-line arguments. Implementations may permit other forms, but are not required to -- but all such forms must return int
.
int _tmain()
is specific to Microsoft.
The reason why different implementations support different entry points (or choices of entry points) is that different OSes or environments have different ways of running programs, or different ways of passing argument information into the program.
There are two kinds of C++ implementation:
"hosted" implementations assume the existence of some kind of OS. On hosted implementations, main
is required in conforming programs and must return int
.
"freestanding" implementations don't assume the existence of an OS. On freestanding implementations it's up to the implementation whether to require main
or not, but the standard does still say that if main
is required then it must return int
.
It's common practice for implementations to provide the facilities of a hosted implementation, but to allow entry points other than main
. This conforms to the standard for a hosted implementation, provided that a conforming program that does define main
is accepted. In effect the implementation allows (as an extension) certain non-conforming programs with no main
function provided they contain instead the implementation-defined alternative. Technically I think it must diagnose the "error", but in practice nobody would use such an extension by accident, so they probably don't want to see a diagnostic.
Similarly, a conforming implementation can accept a program containing void main
. Again, for the implementation to conform it must diagnose that the program doesn't conform.
The meaning of a non-conforming program that the implementation accepts anyway, is up to the implementation.
_tmain
is a MS extension. It is an alias for main
in narrow-character builds and wmain
in wide-character builds. wmain
is also a MS extension, it's like main
except that argv
are provided as wide strings instead of narrow strings. So this is an example of an environment where there are two different ways to give arguments to programs, depending whether or not the program handles characters outside the range of narrow characters (i.e. outside the 8-bit code page).