简体   繁体   English

没有#include wchar的GCC C中的前缀L“…”代表什么?

[英]What does the prefix L“…” stand for in GCC C without #including wchar?

That is, why does unsigned short var= L'ÿ' work, but unsigned short var[]= L"ÿ"; 也就是说,为什么unsigned short var= L'ÿ'起作用, unsigned short var[]= L"ÿ"; does not? 才不是?

L'ÿ' is of type wchar_t , which can be implicitly converted into an unsigned short . L'ÿ'wchar_t类型,可以隐式转换为unsigned short L"ÿ" is of type wchar_t[2] , which cannot be implicitly converted into unsigned short[2] . L"ÿ"的类型为wchar_t[2] ,不能将其隐式转换为unsigned short[2]

L is the prefix for wide character literals and wide-character string literals. L是宽字符文字和宽字符字符串文字的前缀。 This is part of the language and not a header. 这是语言的一部分,而不是标头。 It's also not GCC-specific. 它也不是特定于GCC的。 They would be used like so: 它们的用法如下:

wchar_t some_wchar = L'ÿ';
wchar_t *some_wstring = L"ÿ"; // or wchar_t some_wstring[] = L"ÿ";

You can do unsigned short something = L'ÿ'; 您可以做unsigned short something = L'ÿ'; because a conversion is defined from wchar_t to short. 因为定义了从wchar_t到short的转换。 There is not such conversion defined between wchar_t* and short. 在wchar_t *和short之间没有定义这种转换。

wchar_t is just a typedef to one of the standard integer types. wchar_t只是对标准整数类型之一的typedef The compiler implementor choses such a type that is large enough to hold all wide characters. 编译器实现者选择的类型足以容纳所有宽字符。 If you don't include the header, this is still true and L'ß' is well defined, only that you as a programmer don't know what type it has. 如果您不包括标题,那么这仍然是正确的,并且L'ß'定义正确,只是您作为程序员不知道它的类型。

Your initialization to an integer type works because there are rules to convert one into another. 将您初始化为整数类型是可行的,因为存在将一个转换为另一个的规则。 Assigning a wide character string (ie the address of the first address of a wide character array) to an integer pointer is only possible if you guess the integer type to which wchar_t corresponds correctly. 仅当您猜测wchar_t正确对应的整数类型时,才可以将宽字符串(即,宽字符数组的第一个地址的地址)分配给整数指针。 There is no automatic conversion of pointers of different types, unless one of them is void* . 除非其中之一为void* ,否则不会自动转换不同类型的指针。

Chris has already given the correct answer, but I'd like to offer some thoughts on why you may have made the mistake to begin with. 克里斯已经给出了正确的答案,但是我想对为什么您可能一开始就犯了错误提出了一些想法。 On Windows, wchar_t was defined as 16-bit way back in the early days of Unicode where it was intended to be a 16-bit character set. 在Windows上, wchar_t在Unicode早期被定义为16位,原本是16位字符集。 Unfortunately this turned out to be a bad decision (it makes it impossible for the C compiler to support non-BMP Unicode characters in a way that conforms to the C standard), but they were stuck with it. 不幸的是,事实证明这是一个错误的决定(C编译器无法以符合C标准的方式来支持非BMP Unicode字符),但是他们对此一无所知。

Unix systems from the beginning have used 32-bit wchar_t , which of course means short * and wchar_t * are incompatible pointer types. Unix系统从一开始就使用32位wchar_t ,这当然意味着short *wchar_t *是不兼容的指针类型。

For what I remember of C 对于我记得的C

  • 'Y' or whatever is a char and you can cast it into an int and therefore convert it into a L, “ Y”或任何字符,您可以将其转换为int形式,然后将其转换为L形式,
  • "y" is a string constant and you can't translate it into a integer value “ y”是一个字符串常量,您不能将其转换为整数值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM