大小写标签中允许使用下划线吗?
在一些我无法修改的头文件中,我有以下一组定义:
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,您可以使用括号。您不允许拥有诸如
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.是的,正是下划线引起了问题。 FWIW,这里是 C 语言标准的相关部分(草案 n1256):
_
不是整型常量的有效字符;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):
_
is not a valid character for an integer constant;switch
statement: defines the restrictions oncase
labels in aswitch
statement.当它发现在十六进制表示法中无效的
_
时,它认为它需要)
。编译器实际上应该这样抱怨:
case
标签很可能包含括号:It think it expects the
)
when it finds the_
which isn't valid in hexadecimal notation.The compiler should actually complain like this:
case
labels may well contain parentheses: