473,698 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sizeof(TCHAR)

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
6 13953
On Jul 20, 11:12 pm, "Angus" <nos...@gmail.c omwrote:
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
Neelesh Bodas wrote:
:: On Jul 20, 11:12 pm, "Angus" <nos...@gmail.c omwrote:
::: 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

"Bo Persson" <bo*@gmb.dkwrot e in message
news:5g******** *****@mid.indiv idual.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
On Jul 20, 8:12 pm, "Angus" <nos...@gmail.c omwrote:
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*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 22 '07 #5
On Jul 20, 9:21 pm, "Bo Persson" <b...@gmb.dkwro te:
Neelesh Bodas wrote:
:: On Jul 20, 11:12 pm, "Angus" <nos...@gmail.c omwrote:
::: 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*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 22 '07 #6
James Kanze wrote:
: On Jul 20, 9:21 pm, "Bo Persson" <b...@gmb.dkwro te:
:: Neelesh Bodas wrote:
:
:::: On Jul 20, 11:12 pm, "Angus" <nos...@gmail.c omwrote:
::::: 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2438
by: Giulio | last post by:
I've got to take some data from a CSV file, so i need a good string parser to recognize data to store in variables. using google I found the code below. that seems to be a good solution but I had some problems using it, which libraries should I include? Can someone suggest me any other libraries to do what I need? thanx alot Giulio
12
2737
by: Casper | last post by:
How would one go about summing up the memmory custom tree structures occupies in memmory? Example: struct node { struct node *parent; unsigned int nChildCount; string folder; //string class };
12
3882
by: Onega | last post by:
Hi I create a simple win32 project (VC2003, windows2003(English) , and do simple paint in WM_PAINT message, when the project use multi-character set, it is OK. but when I change to UNICODE, some Chinese characters are illegible( I see sizeof(TCHAR)=2 being displayed). Your idea is welcome. case WM_PAINT: hdc = BeginPaint(hWnd, &ps);
6
3303
by: r.z. | last post by:
This piece of code crashes my app: basic_ifstream<TCHARfile(levelfilepath, ios_base::binary); unsigned int name_length; file.read( (TCHAR*)&name_length, sizeof(unsigned int) ); //crashes here - file is open and contains binary data It may take me hours to find out why this is incorrect, so if you know it, please tell me.
3
2480
by: ujjwaltrivedi | last post by:
Can I use any method to allocate /deallocate memory other than new/ delete operators? Actually the problem is while using delete operator in my ".sqc" files the application crashes. It seems the memory location is locked by DB and so it throws an 'unhandled exception'. Following is the code snippet from my application and now i dont wanna touch the basic design. Regards, UvT
1
7464
by: ranju | last post by:
I am trying to spawn a process (say an exe file) with different user crendentials than that of the current user. 1) Called LogonUserEx() to logon the user and recieve a handle to the token that represents the logged-on user. 2) Called ImpersonateLoggedOnUSer() to let the calling thread impersonate the security context of logged on user. 3) Used CreateEnvironmentBlock() to get lpEnv
25
4677
by: Norman Diamond | last post by:
I think the current version of _vsnwprintf_s is broken, in ordinary Windows. I'm not completely sure yet but it looks like this breakage is worse than previously known Windows CE breakage of StringCchPrintf. For Windows CE breakage of StringCchPrintf, since the %S format died instead of converting ANSI to Unicode, a workaround was to call MultiByteToWideChar and then use the %s format. For ordinary Windows breakage of _vsnwprintf_s,...
1
3101
by: dhanashivam | last post by:
Hi All, I have developed a VC+ dll file which provides a product key for my msi setup file. This VC++ dll work fine together with my setup file. but, my problem is, i have given a message box inside the dll for showing error message while an invalid key is entered. but the messagebox is not shown as modal dialog. i must want to show this as modal dialog. in my VC++ method i have passed the MSIHANDLE from the msi file. how can i...
0
4219
by: ramcbe | last post by:
hello, i am very new to vc++.i have the code. #include "windows.h" #include "stdafx.h" // Need the following header files to access the MSI API #include "msi.h" #include "msiquery.h" #pragma comment(lib, "msi.lib")
0
8604
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8895
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7728
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4369
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.