为什么这是一个警告?我认为在很多情况下,使用多字符 int 常量而不是“无意义”数字或定义具有相同值的 const 变量会更清楚。当解析 wave/tiff/其他文件类型时,将读取值与一些 'EVAW'、'data' 等而不是它们对应的值进行比较会更清晰。
示例代码:
int waveHeader = 'EVAW';
为什么会出现警告?
最佳答案
According to the standard (§6.4.4.4/10)
The value of an integer character constant containing more than one character (e.g., 'ab'), [...] is implementation-defined.
long x = '\xde\xad\xbe\xef'; // yes, single quotes
这是有效的 ISO 9899:2011 C。它在带有 -Wall
的 gcc
下编译时没有警告,并且带有 的“多字符字符常量”警告-迂腐
。
来自 Wikipedia :
Multi-character constants (e.g. 'xy') are valid, although rarely useful — they let one store several characters in an integer (e.g. 4 ASCII characters can fit in a 32-bit integer, 8 in a 64-bit one). Since the order in which the characters are packed into one int is not specified, portable use of multi-character constants is difficult.
为了可移植性,不要使用整数类型的多字符常量。
关于c++ - 多字符常量警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25875063/