1. 程式人生 > >c/c++ int _tmain(int argc, _TCHAR* argv[])

c/c++ int _tmain(int argc, _TCHAR* argv[])

在visual c++ 2005/8 中,當選擇編輯一個32位Win32控制檯應用程式時.初始狀態下系統自帶函式:

int _tmain(int argc, _TCHAR* argv[])
  {
  
return0;
  }


  上述Win32控制檯應用程式的入口程式是用來存放機器的一個環境變數的,如:機器名,系統資訊等.
  其中:
  int argc //表示引數個數
  char *argv[] //表示各個引數,字串陣列的每個單元是char*型別的,指向一個c風格字串。
  //_TCHAR型別是寬字元型字串,和我們一般常用的字串不同,它是32位或者更 高的作業系統中所使用的型別.
  出處:

複製程式碼 大氣象   #include <iostream>
  #include 
<string.h>usingnamespace std;
  
void main(int argc,char*argv[],char*envp[])//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
  _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[])