_tmain()和main()

转载自:http://xiaoqianaihuangten.blog.163.com/blog/static/1916020332012073460625/

用过C的人都知道每一个C的程序都会有一个main(),但有时看别人写的程序发现主函数不是int main(),而是int _tmain(),而且头文件也不是<iostream.h>而是<stdafx.h>,会困惑吧?

一起来看看他们有什么关系吧

首先,这个_tmain()是为了支持unicode所使用的main一个别名而已,既然是别名,应该有宏定义过的,在哪里定义的呢?就在那个让你困惑的<stdafx.h>里,有这么两行

#include <stdio.h>
#include <tchar.h>

我们可以在头文件<tchar.h>里找到_tmain的宏定义

#define _tmain main

所以,经过预编译以后, _tmain就变成main了,这下明白了吧

区别大了。

main()是标准C++的函数入口。标准C++的程序入口点函数,默认字符编码格式ANSI
函数签名为:
int main();
int main(int argc, char* argv[]);

_tmain()是微软操作系统(windows)提供的对unicode字符集和ANSI字符集进行自动转换用的程序入口点函数。
函数签名为:
int _tmain(int argc, TCHAR *argv[])
当你程序当前的字符集为unicode时,int _tmain(int argc, TCHAR *argv[])会被翻译成
int wmain(int argc, wchar_t *argv[])
当你程序当前的字符集为ANSI时,int _tmain(int argc, TCHAR *argv[])会被翻译成
int main(int argc, char *argv[])

原文地址:https://www.cnblogs.com/nami/p/2621283.html