By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,911 Members | 1,171 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,911 IT Pros & Developers. It's quick & easy.

sizeof(TCHAR)

P: n/a
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
Jul 20 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Jul 20, 11:12 pm, "Angus" <nos...@gmail.comwrote:
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

Jul 20 '07 #2

P: n/a
Neelesh Bodas wrote:
:: On Jul 20, 11:12 pm, "Angus" <nos...@gmail.comwrote:
::: 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
Jul 20 '07 #3

P: n/a

"Bo Persson" <bo*@gmb.dkwrote in message
news:5g*************@mid.individual.net...
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.
Jul 21 '07 #4

P: n/a
On Jul 20, 8:12 pm, "Angus" <nos...@gmail.comwrote:
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: ja*********@gmail.com
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

Jul 22 '07 #5

P: n/a
On Jul 20, 9:21 pm, "Bo Persson" <b...@gmb.dkwrote:
Neelesh Bodas wrote:
:: On Jul 20, 11:12 pm, "Angus" <nos...@gmail.comwrote:
::: 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: ja*********@gmail.com
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

Jul 22 '07 #6

P: n/a
James Kanze wrote:
: On Jul 20, 9:21 pm, "Bo Persson" <b...@gmb.dkwrote:
:: Neelesh Bodas wrote:
:
:::: On Jul 20, 11:12 pm, "Angus" <nos...@gmail.comwrote:
::::: 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
Jul 22 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.