Home > Backend Development > C++ > What's the Difference Between `main()` and `_tmain()` in C Windows Programming?

What's the Difference Between `main()` and `_tmain()` in C Windows Programming?

Barbara Streisand
Release: 2024-12-17 15:55:10
Original
415 people have browsed it

What's the Difference Between `main()` and `_tmain()` in C   Windows Programming?

Difference between _tmain() and main() in C

In C , the standard program entry point is main(), which accepts either one of these signatures:

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

Microsoft, however, introduced an extension called wmain(), which replaces the second signature with:

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

To ease the transition between Unicode (UTF-16) and their multibyte character set, Microsoft also defined _tmain() which, when Unicode is enabled, is compiled as wmain, and otherwise as main().

Disparity Between Character Handling

The disparity between main() and _tmain() in your example arises from an incorrect usage of main(). wmain() is designed to accept wchar_t arguments, while main() expects char. Since the compiler is lax in enforcing the correct type for main(), the program interprets the array of wchar_t strings as char strings.

In UTF-16, ASCII characters are represented as a pair of bytes, with the ASCII value followed by a null byte. Since x86 CPUs are little-endian, these bytes are swapped. Thus, in a char string, which is null-terminated, your program sees a series of strings, each a single byte long.

Options for Windows Programming

When working with Windows programming, three main options are available:

  • Explicitly enable Unicode by calling wmain(), using the -W variants of Windows API functions, and employing wchar_t instead of char.
  • Explicitly disable Unicode by calling main(), using -A variants of Windows API functions, and using char for strings.
  • Allow both by calling _tmain(), using the suffixless variants of Windows API functions, and using TCHAR instead of char/wchar_t.

It's important to note that these Microsoft-specific extensions do not conform to the C standard, and therefore may not be supported on other platforms.

The above is the detailed content of What's the Difference Between `main()` and `_tmain()` in C Windows Programming?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template