|
Thanks David,
The statement is simple, something like, L"hello". Why do you think it is not a macro? Who to use L in Visual Studio 2003 Windows Console project?
regards,
George
|
|
|
|
|
If it were a macro (i.e. preprocessor directive), it would resolve to something.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Hi David,
If it is not a macro, what is it?
regards,
George
|
|
|
|
|
George_George wrote: If it is not a macro, what is it?
It's simply a prefix to denote wide-string literals.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Thanks David,
I can understand your point. I mean there should be a technical terminology to call it -- besides the term "prefix". So, if it is not macro, what is it? There is not a technical terminology called "prefix".
regards,
George
|
|
|
|
|
George_George wrote: If it is not a macro, what is it?
it's an extended keyword of the language
|
|
|
|
|
Thanks toxcct,
I want to confirm that keyword L could only be used in C++ and not in C. Right?
regards,
George
|
|
|
|
|
George_George wrote: want to confirm that keyword L could only be used in C++ and not in C
then why didn't you asked clearly ?
and why do you bother that much ? why is it that important ?
can't you just search the WEB for that easy info ?
BTW, what is it that you vote everybody to '4' ???
|
|
|
|
|
Thanks toxcct,
I can not find answer from MSDN so I come here. We could only use L in C++? Can not use in C?
regards,
George
|
|
|
|
|
As has been said, L is not a macro...
char narrow [] = "abc";
TCHAR either [] = _T("abc");
wchar_t wide [0] = L"abc";
short a = 1L;
The last example shows that L is also useful for non-chars, etc.
Iain.
|
|
|
|
|
Thanks Iain,
You code can not compile in my VS 2003 environment,
This statement
wchar_t* wide [] = L"abc";
error C2075: 'wide' : array initialization needs curly braces
When I change to L("hello"), there is an error from compiler that L is undefined.
regards,
George
|
|
|
|
|
George_George wrote: can not compile in my VS 2003 environment,
This statement
wchar_t* wide [] = L"abc";
error C2075: 'wide' : array initialization needs curly braces
When I change to L("hello"), there is an error from compiler that L is undefined.
The below works fine with Visual Studio 2005.
char narrow [] = "abc";
wchar_t wide [] = L"abc";
short a = 1L;
Maxwell Chen
|
|
|
|
|
Thanks Maxwell,
I have verified that it is ok.
regards,
George
|
|
|
|
|
Hello everyone,
I think when UNICODE or _UNICODE is defined, when using _T, it could convert characters to wide character.
And I also think LPCSTR should be defined to pointer to wide character when UNICODE or _UNICODE is defined, and LPCSTR should be defined to pointer to multibyte character when UNICODE and _UNICODE are not defined.
But it seems I am wrong for all points. Could anyone explain to me why I get such warnings in Visual Studio 2003?
<br />
#include "windows.h"<br />
#include "TCHAR.h"<br />
<br />
#define UNICODE<br />
#define _UNICODE<br />
<br />
int main (int argc, char** argv)<br />
{<br />
WCHAR* p = _T("hello");<br />
LPCSTR p1 = p;<br />
LPCWSTR p2 = p;<br />
}<br />
warning messages,
warning C4133: 'initializing' : incompatible types - from 'char [6]' to 'WCHAR *'
warning C4133: 'initializing' : incompatible types - from 'WCHAR *' to 'LPCSTR'
thanks in advance,
George
|
|
|
|
|
The warnings are self-explanatory. With LPCSTR p1 = p , you are trying to assign a WCHAR* (an unsigned short* ) to a LPCSTR (a char* ), which is incompatible.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Thanks David,
I think we should use LPCTSTR, which is defined to LPCSTR when UNICODE and _UNICODE are not defined, and defined to LPCWSTR when UNICODE or _UNICODE is defined. Right?
regards,
George
|
|
|
|
|
George_George wrote: Right?
Yes.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Hi David,
A further question,
why the statement has warning message when UNICODE is defined (you can find it from my sample in question)?
WCHAR* p = _T("hello");
warning C4133: 'initializing' : incompatible types - from 'char [6]' to 'WCHAR *'
regards,
George
|
|
|
|
|
I get this error when Unicode is not defined. If Unicode is defined, it compiles fine.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Thanks David,
I have tried that your method works!
regards,
George
|
|
|
|
|
I'm not quite sure why you asked *almost* the same question 20 minutes later...
LPCSTR <-- narrow
LPCWSTR <-- wide
LPCTSTR <-- depends on UNICODE (or _UNICODE, I forget which)
Iain.
|
|
|
|
|
Good reply, Iain!
regards,
George
|
|
|
|
|
Hi Iain,
A further question,
why the statement has warning message when UNICODE is defined (you can find it from my sample in question)?
WCHAR* p = _T("hello");
warning C4133: 'initializing' : incompatible types - from 'char [6]' to 'WCHAR *'
regards,
George
|
|
|
|
|
No idea, I don't use WCHAR. You'd have to look at the headers to see what's going on.
But I'd change the _T("Hello") to L"Hello". If *that* works, then _T is converting to ascii, and you haven't defined _UNICODE or UNICODE (I can never remember which is which, but one is for C, and one for Win32).
The reason you bother with _T is so that the code will be either ascii or unicode, depending on the defines. In which case, you should use TCHAR, as this varies similarly. If you're hard wiring for unicode, then you can write:
wchar_t *p = L"Hello";
instead.
You had a similar issue in another question to to with LPSTR/LPTSTR/LPWSTR. You need to make up your mind whether you're being ascii, dependent on defines, or full on unicode.
This mixing of hard wired and dependent-on-defines code is going to give you errors in both directions...
Iain
|
|
|
|
|
Thanks Iain,
Iain Clarke wrote: one is for C, and one for Win32
What do you mean one is for C and one for Win32? You mean _UNICODE for C? UNICODE for Win32? Could you provide more information please?
regards,
George
|
|
|
|