CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Why TCHAR?

  1. #1
    Join Date
    Apr 2003
    Location
    HangZhou China
    Posts
    11

    Talking Why TCHAR?

    hi everybody:

    I'm reading Programming Windows

    Always see this : static TCHAR szAppName[]=TEXT("KeyView");

    I'm puzzled why "TCHAR" and Why "TEXT" and why not use standard c/c++ ?
    in MSDN it says " The TCHAR data type is a Win32 character string that can be used to describe ANSI, DBCS, or Unicode strings. For ANSI and DBCS platforms, TCHAR is defined as follows:
    typedef char TCHAR;
    For Unicode platforms, TCHAR is defined as synonymous with the WCHAR type.
    "

    can anyone tell me the function of "TEXT" and "TCHAR"?

    The most important ..Are you use that in your progamme?

    Thank you a lot
    Leaning SDK and then any Rad tools

  2. #2
    Join Date
    Aug 2002
    Location
    Cambridge, UK
    Posts
    12

    Re: Why TCHAR?


    can anyone tell me the function of "TEXT" and "TCHAR"?

    The most important ..Are you use that in your progamme?
    TCHAR's are used because not all non-english characters can be found in the standard 8 bit, 256 character set which is what a CHAR is. A TCHAR is 16 bits so it's possible to encode a vast number of non-english characters.

    I use them in my Pocket PC apps only because you have no choice but to. I don't use them at all in my Windows apps because they take up double the space and I've never seen a need for them. I've got "Programming Windows" too but I just ignore all the TCHAR stuff myself.

    Sean.
    Sean O'Connor
    http://www.windowsgames.co.uk

  3. #3
    Join Date
    Apr 2003
    Posts
    1,755

    Smile

    TCHAR is used to simplify the porting of program from UNICODE and ANSI and vice versa.

    for example you, you have a code like this.

    Code:
    char s1[100];
    char s2[100];
    char s2[100];
    If you port this to UNICODE (eg. CE App) you have to change it to this
    Code:
    WCHAR s1[100];
    WCHAR s2[100];
    WCHAR s2[100];
    To simplify it, you define it using TCHAR, for ANSI, just make this
    Code:
    TCHAR s1[100];
    TCHAR s2[100];
    TCHAR s2[100];
    to make it a UNICODE program, add this to your code
    Code:
    #define UNICODE
    and the compiler will do the conversion from char[100] to WCHAR[100]

    That's how it works. The TEXT works the same

    Hope this will help you
    Last edited by rxbagain; May 1st, 2003 at 03:22 AM.

  4. #4
    Join Date
    Apr 2003
    Location
    HangZhou China
    Posts
    11
    Thank all of you very much ...

    helps me a lot

    I'm a newbie .. Known little about windows programming ...
    Leaning SDK and then any Rad tools

  5. #5
    Join Date
    Mar 2002
    Location
    Singapore
    Posts
    37
    rxbagain, your post is really good!

    But I still have some questions:
    When shall I use the unicode?
    What's the default for Windows english version, ASCII or unicode?
    Any difference between Windows 9X, 2000 and XP?

    Thanks!

  6. #6
    Join Date
    Apr 2003
    Posts
    1,755

    Smile

    Win9X by uses the MutiByte character code (ASCII).

    NT platforms (NT, 2000, XP) uses UNICODE by default, but u can as well use the Multibyte characters in your program

    CE uses only UNICODE.

    Unicode programming is used if you want to make a program that will run on UNICODE systems (NT, 200, XP, CE). This will limit your system targets but it will make faster for your program to manipulate strings because it there is no need for the system to translate from ASCII to UNICODE and vice versa.

    Hope this will clarify your concern.

  7. #7
    Join Date
    Mar 2002
    Location
    Singapore
    Posts
    37

    Thumbs up

    Thanks!

  8. #8
    Join Date
    Feb 2003
    Location
    California
    Posts
    334

    Re: Re: Why TCHAR?

    Originally posted by Sean O'Connor
    I don't use them at all in my Windows apps because they take up double the space and I've never seen a need for them.
    This is not true. (Well, you may not see a need for TCHARs, but the first part...)

    In an ANSI build, a TCHAR is just like a char, and does not take up extra space. In a UNICODE build, true, then the TCHARs are converted to wide chars, and take up the extra space. But presumably, you would only build your project in UNICODE if you needed to support unicode. Sure, wide chars take up extra space, but they are worth it if you want to support any language.

    Personally, I only use TCHARs and WCHARs, I never use straight char's anymore.
    Henri Hein
    Principal Engineer, Propel
    Do not credit Propel with my views or opinions.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured