CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2019
    Posts
    11

    TCHAR type when UNICODE is On or Off

    Hello All,


    I have a basic question regarding the TCHAR type; in the winnt.h file, the TCHAR is defined as

    typedef WCHAR TCHAR, *PTCHAR;

    if UNICODE is defined, and as

    typedef char TCHAR, *PTCHAR;

    if UNICODE is not defined.


    So, since "char" is signed char, and "WCHAR" is defined as "unsigned short", this means that TCHAR is signed if UNICODE is not defined and unsigned if UNICODE is defined..

    (This means that if UNICODE is not defined then the range of TCHAR is -128 to 127, and when UNICODE is defined the range of TCHAR is 0 to 65535.)


    Is there any explanation for this ?


    Best,
    D. Narcis

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: TCHAR type when UNICODE is On or Off

    Whether char is signed or unsigned by default is not specified in the c++ standard - it is down to the compiler implementation. If the default on your implementation is signed, and you want char to be unsigned in the code there will be a compile option (/J for MS VS) to have char unsigned.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Feb 2017
    Posts
    677

    Re: TCHAR type when UNICODE is On or Off

    Quote Originally Posted by dnarcistrinca View Post
    Is there any explanation for this ?
    I think it's for historical reasons.

    Way back when C got the char the dominant character coding was 7-bit ASCII and we needed a type for that. Why not make it 8 bit with a sign bit so we can use it as an int too, clever right?

    Then Unicode came along and shouldn't it have a sign bit like char? No that is a stupid idea. Characters aren't signed. No sign bit please.

    So now we have these two different kinds of characters and we need to handle them both. To do that we use the principle of least astonishment. We don't change anything because that would wreak havoc in existing code. Instead we make it possible to handle each character type separate but with a unifying name. Lets introduce TCHAR. Said and done.
    Last edited by wolle; September 28th, 2019 at 04:55 AM.

  4. #4
    Join Date
    Sep 2019
    Posts
    11

    Re: TCHAR type when UNICODE is On or Off

    Quote Originally Posted by wolle View Post
    I think it's for historical reasons.

    Way back when C got the char the dominant character coding was 7-bit ASCII and we needed a type for that. Why not make it 8 bit with a sign bit so we can use it as an int too, clever right?

    Then Unicode came along and why complicate characters with a sign bit? That's a stupid idea isnt it? No sign bit please.

    So now we have these two different kinds of characters and we need to handle them both. To do that we use the principle of least astonishment. We don't change anything because that would wreak havoc in existing code. Instead we make it possible to handle each character type separate but with a unifying name. Lets introduce TCHAR. Said and done.


    Very good answers, both, I appreciate..Best, D. Narcis

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: TCHAR type when UNICODE is On or Off

    Way back when C got the char the dominant character coding was 7-bit ASCII and we needed a type for that. Why not make it 8 bit with a sign bit so we can use it as an int too, clever right?
    The type of char isn't defined in the C++ standard. This is historical as C++ is derived from C and originally C had a lot of 'undefined' so that the language could be more easily implemented across a broad range of computer hardware in the most easy manner for the compiler implementers. This has led to issues for x-compilation across different compilers - things like the type/size of char (it needn't be 1 byte!), the size of int etc.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

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