为什么会出现_tmain与stdafx.h
2009-07-20 20:33
113 查看
在visual c++ 2008 中,当选择编辑一个32位Win32控制台应用程序时.初始状态下系统自带函数:
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
上述Win32控制台应用程序的入口程序是用来存放机器的一个环境变量的,如:机器名,系统信息等.
其中:
int argc //参数个数
char *argv[] //字符串数组,字符串数组的每个单元是char*类型的,指向一个c风格字符串。
//_TCHAR类型是宽字符型字符串,和我们一般常用的字符串不同,它是32位或者更高的操作系统中所使用的类型.
出处:
#include <iostream>
#include <string.h>
using namespace std;
void main(int argc,char*argv[],char*envp[])
{
int iNumberLines=0; // Default is no line numbers.
// If more than .EXE filename supplied, and if the
// /n command-line option is specified, the listing
// of environment variables is line-numbered.
if(argc==2&&stricmp(argv[1],"/n")==0)
{
iNumberLines=1;
} // Walk through list of strings until a NULL is encountered.
for(int i=0;envp[i]!=NULL;++i )
{
if(!iNumberLines)
cout<<i<<":"<<envp[i]<<"/n";
}
}
The envp parameter is a pointer to an array of null-terminated strings that represent the values set in the user’s environment variables ------这是MSDN的原话.
_tmain:
1. Main是所有c或c++的程序执行的起点,_tmain是main为了支持unicode所使用的main的別名 ._tmain()不过是unicode版本的的main() .
2. _tmain需要一个返回值,而main默认为void.
3. _tmain的定义在<tchar.h>可以找到,如#define _tmain main,所以要加
#include <tchar.h>才能用。_tmain()是个宏,如果是UNICODE则他是wmain()否则他是main().
4. (一般_t、_T、T()这些东西都是宏都和unicode有关系),对于使用非unicode字符集的工程来说,实际上和main没有差别(其实就算是使用unicode字符集也未必有多大的差别)。
5. 因此_tmain compile后仍为main,所以都可以执行.
main()是WINDOWS的控制台程序(32BIT)或DOS程序(16BIT).
WinMain()是WINDOWS的GUI程序.
另外,wmain也是main的另一個别名,是为了支持二个字节的语言环境
-----------------------
int main( int argc[ , char *argv[ ] [, char *envp[ ] ] ] );
wmain( int argc, wchar_t *argv[ ], wchar_t *envp[ ] )
int _tmain(int argc, _TCHAR* argv[])
出处:
You can also use _tmain, which is defined in TCHAR.h. _tmain will resolve to main unless _UNICODE is defined, in which case _tmain will resolve to wmain.---------------------------------------------MSDN原话
http://hi.baidu.com/bytelin/blog/item/df37ccef33e30f3fadafd5ea.html
用过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了,这下明白了吧
btw,<stdafx.h>是用来预编译用的头文件,旨在减小文件的大小和减少编译的时间,想进一步了解,请看百科
http://baike.baidu.com/view/1344257.html?wtp=tt
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
上述Win32控制台应用程序的入口程序是用来存放机器的一个环境变量的,如:机器名,系统信息等.
其中:
int argc //参数个数
char *argv[] //字符串数组,字符串数组的每个单元是char*类型的,指向一个c风格字符串。
//_TCHAR类型是宽字符型字符串,和我们一般常用的字符串不同,它是32位或者更高的操作系统中所使用的类型.
出处:
#include <iostream>
#include <string.h>
using namespace std;
void main(int argc,char*argv[],char*envp[])
{
int iNumberLines=0; // Default is no line numbers.
// If more than .EXE filename supplied, and if the
// /n command-line option is specified, the listing
// of environment variables is line-numbered.
if(argc==2&&stricmp(argv[1],"/n")==0)
{
iNumberLines=1;
} // Walk through list of strings until a NULL is encountered.
for(int i=0;envp[i]!=NULL;++i )
{
if(!iNumberLines)
cout<<i<<":"<<envp[i]<<"/n";
}
}
The envp parameter is a pointer to an array of null-terminated strings that represent the values set in the user’s environment variables ------这是MSDN的原话.
_tmain:
1. Main是所有c或c++的程序执行的起点,_tmain是main为了支持unicode所使用的main的別名 ._tmain()不过是unicode版本的的main() .
2. _tmain需要一个返回值,而main默认为void.
3. _tmain的定义在<tchar.h>可以找到,如#define _tmain main,所以要加
#include <tchar.h>才能用。_tmain()是个宏,如果是UNICODE则他是wmain()否则他是main().
4. (一般_t、_T、T()这些东西都是宏都和unicode有关系),对于使用非unicode字符集的工程来说,实际上和main没有差别(其实就算是使用unicode字符集也未必有多大的差别)。
5. 因此_tmain compile后仍为main,所以都可以执行.
main()是WINDOWS的控制台程序(32BIT)或DOS程序(16BIT).
WinMain()是WINDOWS的GUI程序.
另外,wmain也是main的另一個别名,是为了支持二个字节的语言环境
-----------------------
int main( int argc[ , char *argv[ ] [, char *envp[ ] ] ] );
wmain( int argc, wchar_t *argv[ ], wchar_t *envp[ ] )
int _tmain(int argc, _TCHAR* argv[])
出处:
You can also use _tmain, which is defined in TCHAR.h. _tmain will resolve to main unless _UNICODE is defined, in which case _tmain will resolve to wmain.---------------------------------------------MSDN原话
http://hi.baidu.com/bytelin/blog/item/df37ccef33e30f3fadafd5ea.html
用过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了,这下明白了吧
btw,<stdafx.h>是用来预编译用的头文件,旨在减小文件的大小和减少编译的时间,想进一步了解,请看百科
http://baike.baidu.com/view/1344257.html?wtp=tt
相关文章推荐
- 为什么会出现 java.util.ConcurrentModificationException 异常?
- 为什么执行JAVA程序时,会出现Exception in thread"main" java.lang.NoClassDefFoundError
- 请教:为什么在使用.net中会出现事件丢失的情况?
- 为什么会出现ASP.NET平台下的MVC框架?
- 调用CachedRowSetImpl类时,为什么会出现这种错误
- 为什么会出现ASP.NET平台下的MVC框架?(转)
- 关于 Eclipse 创建 Dynamic Web Project (动态的 WEB 项目) 为什么会出现没有web.xml配置文件的问题
- mybatis执行sql的时候为什么会出现Parameter index out of range (1 > number of parameters, which is 0)?
- php for 循环a到z为什么会出现这么奇怪的现象?
- win2003sp1 安装 rising 为什么会出现kerio的错误
- 为什么jquery的点击事件在click事件下a标签选项卡会出现点击了没有任何反应?
- 为什么会出现 java.util.ConcurrentModificationException 异常?
- 为什么会出现LNK2005"符号已定义"的链接错误?
- vc为什么会出现输出结果为0的情况(异常分析)
- Android和iOS中Cocos2D日志为什么会出现skip frames
- js为什么运行完后会出现undefined
- vs中打空格为什么会出现点
- VC6.0为什么在创建有些对话框时会出现这样的提示:
- 【转】为什么会出现LNK2005"符号已定义"的链接错误?
- servlet为什么会出现?servlet有什么作用?