tags:

views:

42

answers:

3

Hi all

Recently, I meet some tasks about the char/string on windows platform. I see that they are different char type like char, TCHAR, WCHAR, LPSTR, LPWSTR, LPCTSTR. Can someone give me some information about it? And how to use like the regular char and char *. I cam confused about these types?

Best Regards,

+8  A: 

They are documented on MSDN. Here's a few:

  • TCHAR: A WCHAR if UNICODE is defined, a CHAR otherwise.
  • WCHAR: A 16-bit Unicode character.
  • CHAR: An 8-bit Windows (ANSI) character.
  • LPTSTR: An LPWSTR if UNICODE is defined, an LPSTR otherwise.
  • LPSTR: A pointer to a null-terminated string of 8-bit Windows (ANSI) characters.
  • LPWSTR: A pointer to a null-terminated string of 16-bit Unicode characters.
  • LPCTSTR: An LPCWSTR if UNICODE is defined, an LPCSTR otherwise.
  • LPCWSTR: A pointer to a constant null-terminated string of 16-bit Unicode characters.
  • LPCSTR: A pointer to a constant null-terminated string of 8-bit Windows (ANSI) characters.

Note that some of these types map to something different depending on whether UNICODE has been #define'd. By default, they resolve to the ANSI versions:

#include <windows.h>
// LPCTSTR resolves to LPCSTR

When you #define UNICODE before #include <windows.h>, they resolve to the Unicode versions.

#define UNICODE
#include <windows.h>
// LPCTSTR resolves to LPCWSTR

They are in reality typedefs to some fundamental types in the C and C++ language. For example:

typedef char CHAR;
typedef wchar_t WCHAR;

On compilers like Visual C++, there's really no difference between an LPCSTR and a const char* or a LPCWSTR and a const wchar_t* . This might differ between compilers however, which is why these data types exist in the first place!

It's sort of like the Windows API equivalent of <cstdint> or <stdint.h>. The Windows API has bindings in other languages, and having data types with a known size is useful, if not required.

In silico
What do you mean "UNICODE" is defined?
Yongwei Xing
+2  A: 

char is the standard 8-bit character type.

wchar_t is a 16-bit Unicode UTF-16 character type, used since about Windows 95. WCHAR is another name for it.

TCHAR can be either one, depending on your compiler settings. Most of the time in a modern program it's wchar_t.

The P and LP prefixes are pointers to the different types. The L is legacy (stands for Long pointer), and became obsolete with Windows 95; you still see it quite a bit though.

The C after the prefix stands for const.

Mark Ransom
A: 

TCHAR, LPTSTR and LPCTSTR are all generalized macros that will be either regular character strings or wide character strings depending on whether or not the UNICODE define is set. CHAR, LPSTR and LPCSTR are regular character strings. WCHAR, LPWSTR and LPCWSTR are wide character strings. TCHAR, CHAR and WCHAR represents a single character. LPTSTR, LPSTR and LPWSTR are "Long Pointer to STRing". LPCTSTR, LPCSTR and LPWCSTR are constant string pointers.

Emil H