Windows風格與C/C++風格:UNICODE VS _UNICODE 與 TEXT() VS _T()

來源:互聯網
上載者:User
轉自:http://www.mouseos.com/win64/TEXT_T.html

在 Windows 上編程,當使用字串時常會使用:

  1. TEXT()
  2. _T()

這兩個宏作用是對字串常量進行分類,下面的代碼中:

LPTSTR lpStrA = TEXT("Hello");

LPTSTR lpStrB = _T("Hello");

使用 TEXT() _T() 效果是一樣的。

然而,它們卻代表了兩種不同的編程風格:

  1. Windows 編程風格
  2. C/C++ 編程風格

這兩種風格的典型意義是:

_TCHAR *buf = _T("hello");

or

LPTSTR lpBuf = TEXT("hello");

TEXT() 宏定義在 Windows 的標頭檔 WinNT.h 裡,_T() 宏定義 Visual VC/C++ 的標頭檔 tchar.h 裡,因此:TEXT() 代表了windows 編程風格,_T() 代表了 C/C++ 風格。

 

WinNT.h 標頭檔裡,使用 UNICODE 定義:

   1: #ifdef UNICODE

   2: ... ...

   3: #define __TEXT(quote) L##quote 

   4: /* for UNICODE */

   5: ... ...

   6: #else

   7: ... ...

   8: #define __TEXT(quote) quote 

   9: /* for ANSI */

  10: ... ...

  11: #endif

  12: #define TEXT(quote) __TEXT(quote)

 

TEXT() 在定義 UNICODE 情況下使用 UNICODE 字元,否則使用 ANSI 字元。

 

tchar.h 標頭檔裡,使用 _UNCODE 定義:

   1: #ifdef _UNICODE

   2: ... ...

   3: #define __T(x) L##x 

   4: /* for UNICODE */

   5: ... ...

   6: #else

   7: ... ...

   8: #define __T(x) x 

   9: /* for ANSI */

  10: ... ...

  11: #endif

  12: #define _T(x) __T(x)

在編程中你應該始終保持你的編程風格,建議不要混用 TEXT() 與 _T() 宏,雖然它們結果是一樣的。

然而,在 Windows 上編程,我建議你始終保持 windows 編程風格,與系統保持一致性。

因此,我建議:使用 TEXT() 宏。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.