大小写标签中允许使用下划线吗?

发布于 2024-10-16 13:09:30 字数 545 浏览 3 评论 0原文

在一些我无法修改的头文件中,我有以下一组定义:

#define FLAG1                      (0x0000_0001)
#define FLAG2                      (0x0000_0002)
...

然后,在我的代码中,我在 switch 中使用它们:

switch (aaa) {
    case FLAG1:
    ....

    case FLAG2:
    ....
}

因此,Coverity 报告每个案例标签有 2 个缺陷:

RW.EXP_RPAREN: 
Event exp_rparen: expected a ")"

RW.CASE_LABEL_CONFLICT:
Event case_label_conflict: case label value has already appeared in 
this switch at line XX

这些案例标签有什么问题?是否违反C标准?

In some header file which I can not modify I have the following set of defines:

#define FLAG1                      (0x0000_0001)
#define FLAG2                      (0x0000_0002)
...

Then, in my code I use them in switch:

switch (aaa) {
    case FLAG1:
    ....

    case FLAG2:
    ....
}

As a result, Coverity reports on 2 defects per each case label:

RW.EXP_RPAREN: 
Event exp_rparen: expected a ")"

RW.CASE_LABEL_CONFLICT:
Event case_label_conflict: case label value has already appeared in 
this switch at line XX

What is wrong with these case labels? Does it violate C standards?

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

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

发布评论

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

评论(3

百合的盛世恋 2024-10-23 13:09:30

是的,您可以使用括号。您不允许拥有诸如0x0000_0001之类的数字。

这不是有效的十六进制数字,您可能必须删除 _ 字符。

Yes, you are allowed to have parentheses. What you aren't allowed to have is a number like 0x0000_0001.

That's not a valid hex number, you'll probably have to remove the _ character.

白衬杉格子梦 2024-10-23 13:09:30

是的,正是下划线引起了问题。 FWIW,这里是 C 语言标准的相关部分(草案 n1256):

  • § 6.4.4.1 整型常量:定义整型常量的结构并表明 _ 不是整型常量的有效字符;
  • § 6.6 常量表达式:定义常量表达式的限制;
  • § 6.8.4.2 switch 语句:定义switch 语句中case 标签的限制。

Yeah, it's the underscore that's causing problems. FWIW, here are the relevant sections from the C language standard (draft n1256):

  • § 6.4.4.1 Integer Constants: defines the structure for integer constants and shows that _ is not a valid character for an integer constant;
  • § 6.6 Constant Expressions: defines the restrictions on constant expressions;
  • § 6.8.4.2 The switch statement: defines the restrictions on case labels in a switch statement.
话少心凉 2024-10-23 13:09:30

当它发现在十六进制表示法中无效的 _ 时,它认为它需要 )

编译器实际上应该这样抱怨:

error: invalid suffix "_0001" on integer constant

case 标签很可能包含括号:

switch(number) {
    // no way to determine operator 
    // precedence without parens here
    case 2*(1+2):
}

It think it expects the ) when it finds the _ which isn't valid in hexadecimal notation.

The compiler should actually complain like this:

error: invalid suffix "_0001" on integer constant

case labels may well contain parentheses:

switch(number) {
    // no way to determine operator 
    // precedence without parens here
    case 2*(1+2):
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文