C语言 字符常量中的多个字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6944730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple characters in a character constant
提问by Chankey Pathak
Some C compilers permit multiple characters in a character constant. This means that writing 'yes' instead of "yes" may well go undetected. Source: C traps and pitfalls
一些 C 编译器允许在一个字符常量中包含多个字符。这意味着写“是”而不是“是”很可能不会被发现。来源:C 陷阱和陷阱
Can anyone give an example of this where multiple characters are allowed in a character constant?
任何人都可以举一个例子,其中允许在字符常量中使用多个字符?
回答by AProgrammer
As Code Monkey cited, it is implementation defined and implementation varies -- it isn't just a BigEndian/LittleEndian and charset difference. I've tested four implementations (all using ASCII) with the program
正如 Code Monkey 所引用的那样,它是实现定义的,并且实现各不相同——它不仅仅是 BigEndian/LittleEndian 和字符集差异。我已经用程序测试了四种实现(都使用 ASCII)
#include <stdio.h>
int main()
{
unsigned value = 'ABCD';
char* ptr = (char*)&value;
printf("'ABCD' = %02x%02x%02x%02x = %08x\n", ptr[0], ptr[1], ptr[2], ptr[3], value);
value = 'ABC';
printf("'ABC' = %02x%02x%02x%02x = %08x\n", ptr[0], ptr[1], ptr[2], ptr[3], value);
return 0;
}
and I got four different results
我得到了四种不同的结果
Big endian (AIX, POWER, IBM compiler)
大端(AIX、POWER、IBM 编译器)
'ABCD' = 41424344 = 41424344
'ABC' = 00414243 = 00414243
Big endian (Solaris, Sparc, SUN compiler)
大端(Solaris、Sparc、SUN 编译器)
'ABCD' = 44434241 = 44434241
'ABC' = 00434241 = 00434241
Little endian (Linux, x86_64, gcc)
小端(Linux、x86_64、gcc)
'ABCD' = 44434241 = 41424344
'ABC' = 43424100 = 00414243
Little endian (Solaris, x86_64, Sun compiler)
小端(Solaris、x86_64、Sun 编译器)
'ABCD' = 41424344 = 44434241
'ABC' = 41424300 = 00434241
回答by Chankey Pathak
You could use it in a case statement, I guess, but I wouldn't recommend it.
我想你可以在 case 语句中使用它,但我不推荐它。
'yes'is a multicharacter constant. Its type is int, and its value is implementation dependent. So like you already stated, it's up to the compiler.
'yes'是一个多字符常量。它的类型是int,其值取决于实现。所以就像你已经说过的那样,这取决于编译器。
so int foo = 'yes';
所以 int foo = 'yes';
ARM, section 2.5.2, page 9:
ARM,第 2.5.2 节,第 9 页:
"A character constant is one or more characters enclosed in single quotes, as in 'x'."
“字符常量是用单引号括起来的一个或多个字符,如'x'。”
Later on the same page:
稍后在同一页面上:
"Multicharacter constants have type int. The value of a multicharacter constant is implementation dependent. For example, the value of 'AB' could reasonably be expected to be 'A' 'B' and ('A'<<8)+'B' on three different implementations. Multicharacter constants are usually best avoided."
“多字符常量的类型为 int。多字符常量的值取决于实现。例如,可以合理地预期 'AB' 的值是 'A' 'B' 和 ('A'<<8)+'B ' 在三种不同的实现中。通常最好避免使用多字符常量。”
and
和
Quoting from the ANSI C specification (to which C++ makes some attempt to be compatible):
引用 ANSI C 规范(C++ 试图与之兼容):
3.1.3.4 Character Constants Semantics
An integer charcter constant has type int [note that it has type char in C++]...The value of an integer character constant containing more than one character...is implementation-defined.
3.1.3.4 字符常量语义
整数字符常量的类型为 int [注意它在 C++ 中为 char 类型]...包含多个字符的整数字符常量的值...是实现定义的。
回答by Keith Thompson
Multi-character constants are allowedin all contexts where single-character constants are allowed.
在允许使用单字符常量的所有上下文中都允许使用多字符常量。
As for where they'd actually be used, I've seen code that uses multi-character constants to create legible unique values. For example, assuming that int is 4 bytes, 'ABCD' and 'EFGH' are likely to be distinct. (This isn't guaranteed by the language; the implementation must document the mapping, but it needn't be reasonable.) And assuming a reasonable mapping, you'll likely see "ABCD" or "EFGH" in the object code. Not the best idea in the world, but it can work if you don't care much about portability.
至于它们实际使用的地方,我见过使用多字符常量创建清晰唯一值的代码。例如,假设 int 是 4 个字节,则 'ABCD' 和 'EFGH' 很可能是不同的。(这不是语言保证的;实现必须记录映射,但它不必是合理的。)假设一个合理的映射,您可能会在目标代码中看到“ABCD”或“EFGH”。这不是世界上最好的主意,但如果您不太关心便携性,它可以奏效。
Incidentally, allconforming C compilers support multi-character constants (by definition; a compiler that doesn't support them is non-conforming).
顺便说一句,所有符合标准的 C 编译器都支持多字符常量(根据定义;不支持它们的编译器是不符合标准的)。

