8

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?

Community
  • 1
  • 1
Sivaraman
  • 438
  • 2
  • 9
  • AFAIK _tmain is Microsoft specific... – PiotrNycz Oct 15 '12 at 09:16
  • 4
    http://stackoverflow.com/questions/895827/what-is-the-difference-between-tmain-and-main-in-c – Sankalp Oct 15 '12 at 09:16
  • And `void main()` is very old C++. – PiotrNycz Oct 15 '12 at 09:17
  • 3
    `void main()` was *never* valid C++. – Keith Thompson Oct 15 '12 at 09:18
  • http://stackoverflow.com/questions/895827/what-is-the-difference-between-tmain-and-main-in-c – Philluminati Oct 15 '12 at 09:18
  • 2
    `void main()` has also never been valid C (except that an implementation may support it as an extension). The same standard (the 1989 ANSI C standard) that introduced the `void` keyword defined the two valid forms: `int main(void)` and `int main(int argc, char *argv[])` or equivalent. Certain book authors invented the false idea that `void main()` is valid; I'd like to know why. – Keith Thompson Oct 15 '12 at 09:25
  • @Keith: I assume that they had previously been writing `main` functions with implicit-int return value, that fell off the end of the function instead of returning a defined value. At some point their compiler complained about a non-void function not returning a value, and the laziest possible way to "fix" that was to change `main` to return `void`, disregarding that it's still not valid. I believe that a common implementation detail to support both required forms of `main` is just to not check the signature of `main` at all. – Steve Jessop Oct 15 '12 at 09:38
  • @SteveJessop: Would it have been enough to just add int? As the standard explicitly mentions that the return statement is not necessary => return 0 [§3.6.1.5] – MFH Oct 15 '12 at 12:40
  • @MFH: the C99 standard says that, but we're talking about the era of the C89 standardization. – Steve Jessop Oct 15 '12 at 13:02

2 Answers2

5

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.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
2

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

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699