What is the difference between main () and _ tmain ()? What is the difference between main_tmain?

Source: Internet
Author: User

What is the difference between main () and _ tmain ()? What is the difference between main_tmain?

People who have used C know that every C program has a main (), but sometimes it is found that the main function is not int main (), but int _ tmain (), the header file is not <iostream. h> but <stdafx. h> Are you confused?
Let's take a look at their relationships.
First, this _ tmain () is an alias for the main used in unicode. Since it is an alias, there should be macro-defined. Where should we define it? In the confusing <stdafx. h>, there are two rows.
# Include <stdio. h>
# Include <tchar. h>
We can find the macro definition of _ tmain in the header file <tchar. h>.
# Define _ tmain main
Therefore, after pre-compilation, _ tmain is changed to main.


I can see the difference between tmain and main on the Internet. The translation is messy. By the way, I can translate it:

People who have used C language know that every C program has a main function, but sometimes the _ tmain function appears in programs written by others, which will be very confusing. I was confused at the time, so I went online to find information. Now I will summarize the results. This is because there is no evidence. If it is incorrect. Please note: _ tmain and main

[@ More @]

1. Main is a function of the C program, and _ tmain is a function of the main used to support unicode.
2. The definition of _ tmain can be found in <tchar. h>, for example, # define _ tmain main. Therefore, you must add # I nclude <tchar. h> to use it.

3. Therefore, after _ tmain compile is still main, You can execute

In addition, wmain is another alias of main to support the two-byte language environment.
Int main (int argc [, char * argv [] [, char * envp []);
Wmain (int argc, wchar_t * argv [], wchar_t * envp [])
Int _ tmain (int argc, _ TCHAR * argv [])


What are the advantages of Replacing main () with _ tmain?
_ Tmain () Can I decide whether the arcv [] character is ANSY or UNICODE when the program is started using the command line?
Can you tell me what you know?
UNICODE is supported. Actually reflected in? I use the TCHAR and generic functions in the main () WinMain () function, so that the source code can be used to compile ASNY and UNICODE respectively. So I asked whether the difference between _ tmain () is only when the program is started?
Whether UNICODE is used is not determined when the program is started, but whether the "UNICODE" is pre-defined during program compilation ", therefore, the function of _ tmain is the same as that of using TCHAR instead of a specific char or w_char. When UNICODE is defined during compilation, it means that wmain is main when not defined,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.