日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

為什么會出現(xiàn)_tmain與stdafx.h

 埃德溫會館 2013-01-27

為什么會出現(xiàn)_tmain與stdafx.h

488人閱讀 評論(1) 收藏 舉報

在visual c++ 2008 中,當選擇編輯一個32位Win32控制臺應用程序時.初始狀態(tài)下系統(tǒng)自帶函數(shù):

int _tmain(int argc, _TCHAR* argv[])

{

return 0;

}

上述Win32控制臺應用程序的入口程序是用來存放機器的一個環(huán)境變量的,如:機器名,系統(tǒng)信息等.  

其中:

int argc               //參數(shù)個數(shù)

char *argv[]          //字符串數(shù)組,字符串數(shù)組的每個單元是char*類型的,指向一個c風格字符串。

                             //_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í)行的起點,_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,所以都可以執(zhí)行.

main()是WINDOWS的控制臺程序(32BIT)或DOS程序(16BIT).

WinMain()是WINDOWS的GUI程序.

另外,wmain也是main的另一個別名,是為了支持二個字節(jié)的語言環(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

用過C的人都知道每一個C的程序都會有一個main(),但有時看別人寫的程序發(fā)現(xiàn)主函數(shù)不是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

    本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多