多字符持续警告

发布于 2024-12-09 19:17:55 字数 214 浏览 0 评论 0原文

为什么这是一个警告?我认为在很多情况下,使用多字符 int 常量而不是“无意义”的数字或定义具有相同值的 const 变量会更清楚。当解析wave/tiff/其他文件类型时,将读取的值与一些“EVAW”、“数据”等而不是其相应的值进行比较会更清楚。

示例代码:

int waveHeader = 'EVAW';

为什么会发出警告?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

や三分注定 2024-12-16 19:17:55

根据标准 (§6.4.4.4/ 10)

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

long x = '\xde\xad\xbe\xef'; // yes, single quotes

这是有效的 ISO 9899:2011 C。它在使用 -Wallgcc 下编译时没有警告,使用 -pedantic< 则出现“多字符字符常量”警告。 /代码>。

来自维基百科

多字符常量(例如“xy”)是有效的,尽管很少
有用——它们让人们在一个整数中存储几个字符(例如 4
32 位整数可容纳 ASCII 字符,64 位整数可容纳 8 个)。
由于字符打包到一个 int 中的顺序不是
指定、可移植地使用多字符常量是很困难的。

为了可移植性,不要使用具有整型的多字符常量。

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:2011 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.

风轻花落早 2024-12-16 19:17:55

此警告对于那些在应该编写 "test" 的地方错误地编写 'test' 的程序员很有用。

这种情况比真正需要多字符 int 常量的程序员更常见。

This warning is useful for programmers that would mistakenly write 'test' where they should have written "test".

This happen much more often than programmers that do actually want multi-char int constants.

禾厶谷欠 2024-12-16 19:17:55

如果您很高兴知道自己在做什么并且可以接受可移植性问题,例如在 GCC 上,您可以禁用命令行上的警告:

-Wno-multichar

我将其用于我自己的应用程序,以处理类似的 AVI 和 MP4 文件头原因给你。

If you're happy you know what you're doing and can accept the portability problems, on GCC for example you can disable the warning on the command line:

-Wno-multichar

I use this for my own apps to work with AVI and MP4 file headers for similar reasons to you.

通知家属抬走 2024-12-16 19:17:55

即使您愿意查找您的实现定义的行为,多字符常量仍然会随字节顺序而变化。

最好使用 (POD) struct { char[4] }; ...然后使用像“WAVE”_4cc 这样的 UDL 轻松构造该类的实例

Even if you're willing to look up what behavior your implementation defines, multi-character constants will still vary with endianness.

Better to use a (POD) struct { char[4] }; ... and then use a UDL like "WAVE"_4cc to easily construct instances of that class

还不是爱你 2024-12-16 19:17:55

@leftaroundabout 在上面的评论中提到了最简单的 C/C++ 任何编译器/标准兼容解决方案:

int x = *(int*)"abcd";

或者更具体一点:

int x = *(int32_t*)"abcd";

还有一个解决方案,自 C99 以来也符合 C/C++ 编译器/标准(clang++ 除外,其中 有一个已知错误):

int x = ((union {char s[5]; int number;}){"abcd"}).number;

/* just a demo check: */
printf("x=%d stored %s byte first\n", x, x==0x61626364 ? "MSB":"LSB");

这里匿名联合用于为所需的数字结果提供一个漂亮的符号名称,“abcd”字符串用于初始化 复合文字 (C99)。

Simplest C/C++ any compiler/standard compliant solution, was mentioned by @leftaroundabout in comments above:

int x = *(int*)"abcd";

Or a bit more specific:

int x = *(int32_t*)"abcd";

One more solution, also compliant with C/C++ compiler/standard since C99 (except clang++, which has a known bug):

int x = ((union {char s[5]; int number;}){"abcd"}).number;

/* just a demo check: */
printf("x=%d stored %s byte first\n", x, x==0x61626364 ? "MSB":"LSB");

Here anonymous union is used to give a nice symbol-name to the desired numeric result, "abcd" string is used to initialize the lvalue of compound literal (C99).

拥醉 2024-12-16 19:17:55

如果您想禁用此警告,请务必了解 GCC 和 Clang 中有两个相关的警告参数: GCC 编译器选项 -wno-四字符常量和 -wno-multichar

If you want to disable this warning it is important to know that there are two related warning parameters in GCC and Clang: GCC Compiler options -wno-four-char-constants and -wno-multichar

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文