_tmain()

來源:互聯網
上載者:User

在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原話

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.