Difference Between _Tmain() and Main() in C++

What is the difference between _tmain() and main() in C++?

_tmain does not exist in C++. main does.

_tmain is a Microsoft extension.

main is, according to the C++ standard, the program's entry point.
It has one of these two signatures:

int main();
int main(int argc, char* argv[]);

Microsoft has added a wmain which replaces the second signature with this:

int wmain(int argc, wchar_t* argv[]);

And then, to make it easier to switch between Unicode (UTF-16) and their multibyte character set, they've defined _tmain which, if Unicode is enabled, is compiled as wmain, and otherwise as main.

As for the second part of your question, the first part of the puzzle is that your main function is wrong. wmain should take a wchar_t argument, not char. Since the compiler doesn't enforce this for the main function, you get a program where an array of wchar_t strings are passed to the main function, which interprets them as char strings.

Now, in UTF-16, the character set used by Windows when Unicode is enabled, all the ASCII characters are represented as the pair of bytes \0 followed by the ASCII value.

And since the x86 CPU is little-endian, the order of these bytes are swapped, so that the ASCII value comes first, then followed by a null byte.

And in a char string, how is the string usually terminated? Yep, by a null byte. So your program sees a bunch of strings, each one byte long.

In general, you have three options when doing Windows programming:

  • Explicitly use Unicode (call wmain, and for every Windows API function which takes char-related arguments, call the -W version of the function. Instead of CreateWindow, call CreateWindowW). And instead of using char use wchar_t, and so on
  • Explicitly disable Unicode. Call main, and CreateWindowA, and use char for strings.
  • Allow both. (call _tmain, and CreateWindow, which resolve to main/_tmain and CreateWindowA/CreateWindowW), and use TCHAR instead of char/wchar_t.

The same applies to the string types defined by windows.h:
LPCTSTR resolves to either LPCSTR or LPCWSTR, and for every other type that includes char or wchar_t, a -T- version always exists which can be used instead.

Note that all of this is Microsoft specific. TCHAR is not a standard C++ type, it is a macro defined in windows.h. wmain and _tmain are also defined by Microsoft only.

What are, and what is the difference between, kmain and dmain -- main functions in C?

You seem to be referring to this tutorial on OSDev.org.

If you’ll notice, this is not a standard C program with main() as its entry point. In fact, the page shows two entry points: one written in Assembler in a way for GRUB to find & load, and which sets up & loads the second, kmain(), which is written in C. The writers use the name kmain to mean “kernel main”; presumably dmain is the entry point for drivers & would stand for “driver main”.

C makes a distinction between “freestanding” and “hosted” implementations. Hosted is what you’re probably more familiar with; the standard C library is available, and all programs start at the main function.

OS kernels are (often) good examples of freestanding environments. The C library will likely not be available, for example (except for certain headers like stddef.h & stdarg.h; see the standard for details). Also, the entry point is not defined by the standard anymore. The OSDev.org tutorial is making a special point of that fact, by explicitly defining its entry point with a different name.

You could probably run the tutorial renaming kmain to main, but note that it’s still void main(void*, unsigned int), not int main(int, char**); in fact that sort of confusion is likely part of the reason the writers chose to use a different name. But is is just a convention they’ve selected, not anything standardized.

What is the difference between wmain and main?

"If your code adheres to the Unicode programming model, you can use the wide-character version of main, which is wmain."

http://msdn.microsoft.com/en-us/library/aa299386%28VS.60%29.aspx

main( int argc, char *argv[ ], char *envp[ ] )
{
program-statements
}

wmain( int argc, wchar_t *argv[ ], wchar_t *envp[ ] )
{
program-statements
}

Understanding _tmain in Visual C++ console projects

Take a look here for _tmain... etc.

What is the difference between _tmain() and main() in C++?

stdafx.h is a precompiled header (optional) for Windows applications. More here:

http://en.wikipedia.org/wiki/Precompiled_header

Why do we have different versions of main functions in c++?

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.

Why is there a macro which defines _tmain?

This is a Visual C++-specific feature, it's not a part of C++.

Most of the Windows API functions have two versions: those that end in W, which are for use with wide character strings (wchar_t strings) and those that end in A, which are for use with narrow character strings (char strings). The actual Windows API "functions" don't have any suffix and are defined as macros that expand to the right version depending on the settings.

The T names (like _TCHAR and _tmain) are for the same purpose: they are macros that expand to the right name depending on your compilation settings, so wchar_t and wmain for wide character support, or char and main for narrow character support.

The idea is that if you write your code using the character-type-agnostic names (the T names), you can compile your code to use narrow characters (ASCII) or wide characters (Unicode) without changing it. The tradeoff is that your code is less portable.

getting the right compiler for C++

A minor point, which I don't see elsewhere in the answers: When using precompiled headers, such as your stdafx.h, you need to include them first. Change it to:

#include "stdafx.h"
#include <iostream>

and that should fix the errors about it.

Alternatively, it may be easier to simply switch off precompiled headers: Project > Properties > Configuration Properties > C/C++ > Precompiled Headers > Switch first option to "Not using precompiled headers". They can be useful for big projects but will just be awkward and annoying while you're learning, since they have extra rules (like this "must be included first") which aren't requirements of standard C++ .



Related Topics



Leave a reply



Submit