其他分享
首页 > 其他分享> > c – 多字符恒定警告

c – 多字符恒定警告

作者:互联网

为什么这是一个警告?我认为在很多情况下使用multi-char int常量而不是“无意义”数字或者使用相同值定义const变量更为明确.解析wave / tiff /其他文件类型时,可以更清楚地将读取值与某些“EVAW”,“数据”等进行比较,而不是相应的值.

示例代码:

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,并且使用-pedallic编译“多字符字符常量”警告.

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-3,c,portability,casting,compiler-warnings
来源: https://codeday.me/bug/20190911/1804203.html