多字符常量警告 [英] Multi-character constant warnings

查看:207
本文介绍了多字符常量警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这是一个警告?我觉得有很多情况下更加清晰,而不是使用没有意义的数字或代替定义具有相同的价值了const变量多字符常量INT时。当解析波/ TIFF /其他文件类型更加清晰的读值一些,而不是其相应的价值EVAW','数据'等比较。

样code:

  INT waveHeader ='EVAW';

为什么这给予警告?


解决方案

据标准(§6.4.4.4.10)


  

包含多个整型字符常量的值
  字符(例如,'AB')[...]是实现定义的。


 长×='\\ XDE \\ XAD \\ XBE \\ XEF'; //是的,单引号

这是有效的ISO 9899:1999 C.它编译没有与 -Wall GCC 警告和多字符字符常量的警告与 -pedantic

维基百科


  

多字符常量(例如'XY')是有效的,虽然很少
  有用的 - 他们让一个整数一家店几个字符(例如,4
  ASCII字符可以容纳在一个32位整数,8个在64位的一个)。
  因为在该字符被打包成一个INT的顺序并不
  规定,携带使用多字符常量是困难的。


有关便携性的缘故,不要使用多字符常量整数类型。

Why is this a warning? I think there are many cases when is more clear to use multi-char int constants instead of "no meaning" numbers or instead of defining const variables with same value. When parsing wave/tiff/other file types is more clear to compare the read values with some 'EVAW', 'data', etc instead of their corresponding values.

Sample code:

int waveHeader = 'EVAW';

Why does this give a warning?

解决方案

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

This is valid ISO 9899:1999 C. It compiles without warning under gcc with -Wall, and a "multi-character character constant" warning with -pedantic.

From 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.

For portability sake, don't use multi-character constants with integral types.

这篇关于多字符常量警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆