Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,560 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,907 people online right now. Registration is fast and FREE... Join Now!




what is CHAR and TCHAR and char?

 
Reply to this topicStart new topic

what is CHAR and TCHAR and char?

skyHigh
6 May, 2008 - 01:20 PM
Post #1

D.I.C Head
**

Joined: 1 Oct, 2007
Posts: 87


My Contributions
Can anyone give a brief description of how these data types (CHAR and TCHAR and char) are differences?

CODE

CHAR string1[] = "Function";
TCHAR string2[] = _T("Function");

HANDLE hFile = CreateFile(fileName, GENERIC_READ|GENERIC_WRITE,
        FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

WriteFile(hFile, string1, sizeof(string1), &byteWritten, NULL);
WriteFile(hFile, string2, sizeof(string2), &byteWritten, NULL);



Outputs:
first output is: Function
second output is: F U N C T I O N


Can anyone tell me why the outputs are different? and what are the difference among those data types above?

thanks.

This post has been edited by skyHigh: 6 May, 2008 - 01:28 PM
User is offlineProfile CardPM
+Quote Post

Cerolobo
RE: What Is CHAR And TCHAR And Char?
6 May, 2008 - 02:48 PM
Post #2

D.I.C Regular
Group Icon

Joined: 5 Apr, 2008
Posts: 440



Thanked: 31 times
My Contributions
CHAR is defined as just a char
TCHAR can be defined as a short or a character. Since you you getting two characters per letter, then means that you are compiling you code under Unicode. Unicode is basically the use of 2 bytes to represent 1 character. This is useful for foreign languages, since a lot of foreign counties have alphabets larger then our own.
User is offlineProfile CardPM
+Quote Post

skyHigh
RE: What Is CHAR And TCHAR And Char?
7 May, 2008 - 09:33 AM
Post #3

D.I.C Head
**

Joined: 1 Oct, 2007
Posts: 87


My Contributions
QUOTE(Cerolobo @ 6 May, 2008 - 03:48 PM) *

CHAR is defined as just a char
TCHAR can be defined as a short or a character. Since you you getting two characters per letter, then means that you are compiling you code under Unicode. Unicode is basically the use of 2 bytes to represent 1 character. This is useful for foreign languages, since a lot of foreign counties have alphabets larger then our own.



Thanks for reply. Maybe you are right because char is one byte and CHAR is also one byte then I believe one byte can store one letter; is it right? so in that case TCHAR is two bytes since you said two characters per letter which means the first byte is used to store one letter, and second byte is no used, that is why it produces extra space after each letter. Then I guess that makes sense, but then who knows.
User is offlineProfile CardPM
+Quote Post

Cerolobo
RE: What Is CHAR And TCHAR And Char?
7 May, 2008 - 09:41 AM
Post #4

D.I.C Regular
Group Icon

Joined: 5 Apr, 2008
Posts: 440



Thanked: 31 times
My Contributions
For Microsoft, a CHAR is defined as this

#define CHAR char

A char is just a single byte, which is typically 8 bits, which gives it the ability to hold a value of 0 to 255 if it is unsigned.

Now, take a took at the ASCII Table. As you can see, every symbol is assigned a number out of the 255 range.

Unicode is used, since a lot of other languages have larger character sets then ours. Think of the Japaneses, who have various sets of huge character sets, including katakana, hiragana, kanji, and rōmaji. There is no way they can fit all of those characters in a range of 0 to 255, so Unicode is used, which supports up to 65535 characters.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 11:32PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month