在visual c++ 2008 中,當(dāng)選擇編輯一個(gè)32位Win32控制臺(tái)應(yīng)用程序時(shí).初始狀態(tài)下系統(tǒng)自帶函數(shù):
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
上述Win32控制臺(tái)應(yīng)用程序的入口程序是用來(lái)存放機(jī)器的一個(gè)環(huán)境變量的,如:機(jī)器名,系統(tǒng)信息等.
其中:
int argc //參數(shù)個(gè)數(shù)
char *argv[] //字符串?dāng)?shù)組,字符串?dāng)?shù)組的每個(gè)單元是char*類型的,指向一個(gè)c風(fēng)格字符串。
//_TCHAR類型是寬字符型字符串,和我們一般常用的字符串不同,它是32位或者更高的操作系統(tǒng)中所使用的類型.
出處:
#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++的程序執(zhí)行的起點(diǎn),_tmain是main為了支持unicode所使用的main的別名 ._tmain()不過(guò)是unicode版本的的main() .
2. _tmain需要一個(gè)返回值,而main默認(rèn)為void.
3. _tmain的定義在<tchar.h>可以找到,如#define _tmain main,所以要加
#include <tchar.h>才能用。_tmain()是個(gè)宏,如果是UNICODE則他是wmain()否則他是main().
4. (一般_t、_T、T()這些東西都是宏都和unicode有關(guān)系),對(duì)于使用非unicode字符集的工程來(lái)說(shuō),實(shí)際上和main沒(méi)有差別(其實(shí)就算是使用unicode字符集也未必有多大的差別)。
5. 因此_tmain compile后仍為main,所以都可以執(zhí)行.
main()是WINDOWS的控制臺(tái)程序(32BIT)或DOS程序(16BIT).
WinMain()是WINDOWS的GUI程序.
另外,wmain也是main的另一個(gè)別名,是為了支持二個(gè)字節(jié)的語(yǔ)言環(huán)境
-----------------------
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
用過(guò)C的人都知道每一個(gè)C的程序都會(huì)有一個(gè)main(),但有時(shí)看別人寫的程序發(fā)現(xiàn)主函數(shù)不是int main(),而是int _tmain(),而且頭文件也不是<iostream.h>而是<stdafx.h>,會(huì)困惑吧?
一起來(lái)看看他們有什么關(guān)系吧
首先,這個(gè)_tmain()是為了支持unicode所使用的main一個(gè)別名而已,既然是別名,應(yīng)該有宏定義過(guò)的,在哪里定義的呢?就在那個(gè)讓你困惑的<stdafx.h>里,有這么兩行
#include <stdio.h>
#include <tchar.h>
我們可以在頭文件<tchar.h>里找到_tmain的宏定義
#define _tmain main
所以,經(jīng)過(guò)預(yù)編譯以后, _tmain就變成main了,這下明白了吧
btw,<stdafx.h>是用來(lái)預(yù)編譯用的頭文件,旨在減小文件的大小和減少編譯的時(shí)間,想進(jìn)一步了解,請(qǐng)看百科
聯(lián)系客服