sizeof(TCHAR)

A

Angus

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
 
N

Neelesh Bodas

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
 
B

Bo Persson

Neelesh Bodas 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
 
A

Angus

Bo Persson said:
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.
 
J

James Kanze

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.
 
J

James Kanze

Neelesh Bodas 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.
 
B

Bo Persson

James Kanze wrote:
:: Neelesh Bodas 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top