sizeof(TCHAR)

Discussion in 'C++' started by Angus, Jul 20, 2007.

  1. Angus

    Angus Guest

    On my PC (running Windows) if I ask for sizeof(TCHAR) I get 1 if UNICODE
    defined. Same if UNICODE (and _UNICODE) not defined. I sort of assumed
    that under UNICODE it would be 2? I understand on UNIX it is often 4.

    How can it be 1 for both ASCII AND Unicode?

    Confused
     
    Angus, Jul 20, 2007
    #1
    1. Advertising

  2. On Jul 20, 11:12 pm, "Angus" <> wrote:
    > On my PC (running Windows) if I ask for sizeof(TCHAR) I get 1 if UNICODE
    > defined. Same if UNICODE (and _UNICODE) not defined. I sort of assumed
    > that under UNICODE it would be 2? I understand on UNIX it is often 4.
    >
    > How can it be 1 for both ASCII AND Unicode?
    >
    > Confused


    1. TCHAR is not a fundamental type according to the C++ standard. It
    might be a type defined by your compiler.

    2. sizeof(char) == 1 by definition. For other fundamental types, what
    sizeof() returns is implementation defined.

    -N
     
    Neelesh Bodas, Jul 20, 2007
    #2
    1. Advertising

  3. Angus

    Bo Persson Guest

    Neelesh Bodas wrote:
    :: On Jul 20, 11:12 pm, "Angus" <> wrote:
    ::: On my PC (running Windows) if I ask for sizeof(TCHAR) I get 1 if
    ::: UNICODE defined. Same if UNICODE (and _UNICODE) not defined. I
    ::: sort of assumed that under UNICODE it would be 2? I understand
    ::: on UNIX it is often 4.
    :::
    ::: How can it be 1 for both ASCII AND Unicode?
    :::
    ::: Confused
    ::
    :: 1. TCHAR is not a fundamental type according to the C++ standard.
    :: It might be a type defined by your compiler.
    ::
    :: 2. sizeof(char) == 1 by definition. For other fundamental types,
    :: what sizeof() returns is implementation defined.
    ::

    True. On a very popular Windows compiler, UNICODE and _UNICODE should
    either both be defined, or both be undefined. Otherwise these
    confusing things happen.

    On the other hand, if you are coding for any currently supported
    versions of Windows, you can just use wchar_t as the native character
    type, and skip all this T-stuff.


    Bo Persson
     
    Bo Persson, Jul 20, 2007
    #3
  4. Angus

    Angus Guest

    "Bo Persson" <> wrote in message
    news:...
    > Neelesh Bodas wrote:
    >
    > True. On a very popular Windows compiler, UNICODE and _UNICODE should
    > either both be defined, or both be undefined. Otherwise these
    > confusing things happen.
    >
    > On the other hand, if you are coding for any currently supported
    > versions of Windows, you can just use wchar_t as the native character
    > type, and skip all this T-stuff.
    >
    >
    > Bo Persson
    >
    >

    I had the Unicode defines in the wrong file. With unicode defined,
    sizeof(TCHAR) is 2

    There are still Windows users running unsupported platforms so can't really
    use wchar_t exclusively yet. One day.
     
    Angus, Jul 21, 2007
    #4
  5. Angus

    James Kanze Guest

    On Jul 20, 8:12 pm, "Angus" <> wrote:
    > On my PC (running Windows) if I ask for sizeof(TCHAR) I get 1 if UNICODE
    > defined. Same if UNICODE (and _UNICODE) not defined. I sort of assumed
    > that under UNICODE it would be 2? I understand on UNIX it is often 4.


    It depends on how you've defined it. It's easy to imagine
    definitions where sizeof(TCODE) won't even compile.

    In a C++ standards conformant mode, of course, the symbol cannot
    be predefined. In some non-conformant modes, who knows, but
    I've never heard of a Unix compiler predefining it in any mode.

    --
    James Kanze (Gabi Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jul 22, 2007
    #5
  6. Angus

    James Kanze Guest

    On Jul 20, 9:21 pm, "Bo Persson" <> wrote:
    > Neelesh Bodas wrote:


    > :: On Jul 20, 11:12 pm, "Angus" <> wrote:
    > ::: On my PC (running Windows) if I ask for sizeof(TCHAR) I get 1 if
    > ::: UNICODE defined. Same if UNICODE (and _UNICODE) not defined. I
    > ::: sort of assumed that under UNICODE it would be 2? I understand
    > ::: on UNIX it is often 4.


    > ::: How can it be 1 for both ASCII AND Unicode?


    > :: 1. TCHAR is not a fundamental type according to the C++ standard.
    > :: It might be a type defined by your compiler.


    > :: 2. sizeof(char) == 1 by definition. For other fundamental types,
    > :: what sizeof() returns is implementation defined.


    > True. On a very popular Windows compiler, UNICODE and _UNICODE should
    > either both be defined, or both be undefined. Otherwise these
    > confusing things happen.


    > On the other hand, if you are coding for any currently supported
    > versions of Windows, you can just use wchar_t as the native character
    > type, and skip all this T-stuff.


    wchar_t is a C++ standard type, and available with any C++
    compiler, regardless of the system. Just using wchar_t,
    however, won't automatically make Unicode work anywhere. Using
    wide characters is different from using char, no matter how you
    cut it, and you need to adapt your program accordingly.

    --
    James Kanze (Gabi Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jul 22, 2007
    #6
  7. Angus

    Bo Persson Guest

    James Kanze wrote:
    : On Jul 20, 9:21 pm, "Bo Persson" <> wrote:
    :: Neelesh Bodas wrote:
    :
    :::: On Jul 20, 11:12 pm, "Angus" <> wrote:
    ::::: On my PC (running Windows) if I ask for sizeof(TCHAR) I get 1 if
    ::::: UNICODE defined. Same if UNICODE (and _UNICODE) not defined. I
    ::::: sort of assumed that under UNICODE it would be 2? I understand
    ::::: on UNIX it is often 4.
    :
    ::::: How can it be 1 for both ASCII AND Unicode?
    :
    :::: 1. TCHAR is not a fundamental type according to the C++ standard.
    :::: It might be a type defined by your compiler.
    :
    :::: 2. sizeof(char) == 1 by definition. For other fundamental types,
    :::: what sizeof() returns is implementation defined.
    :
    :: True. On a very popular Windows compiler, UNICODE and _UNICODE
    :: should either both be defined, or both be undefined. Otherwise
    :: these confusing things happen.
    :
    :: On the other hand, if you are coding for any currently supported
    :: versions of Windows, you can just use wchar_t as the native
    :: character type, and skip all this T-stuff.
    :
    : wchar_t is a C++ standard type, and available with any C++
    : compiler, regardless of the system. Just using wchar_t,
    : however, won't automatically make Unicode work anywhere. Using
    : wide characters is different from using char, no matter how you
    : cut it, and you need to adapt your program accordingly.

    Sure.

    In this particular case, it's about using TCHAR that can be either a
    char or a wchar_t in Windows headers. My point was that there is not
    much use for an 8-bit char compile anymore, as all present Windows
    versions use wchar_t natively anyway. So the OP might want to skip
    tons of non-standard macros, and use whar_t directly, as well.

    Support for Windows 9x shouldn't be in high demand for new software,
    as the OSs themselves are unsupported.


    Bo Persson
     
    Bo Persson, Jul 22, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    7
    Views:
    1,273
    MaSTeR
    Dec 14, 2004
  2. Bobby
    Replies:
    0
    Views:
    403
    Bobby
    Dec 14, 2004
  3. ree
    Replies:
    7
    Views:
    3,794
  4. sorty
    Replies:
    4
    Views:
    20,736
    Rolf Magnus
    Nov 25, 2003
  5. r.z.
    Replies:
    6
    Views:
    758
    David Harmon
    Dec 28, 2006
Loading...

Share This Page