给switch限定值c语言,为什么我们不允许在switch-case中使用全局的const限定变量?IBM支持门户网站提示我们可以...

我想你正在读你的自己的消息到那个报价中。

An object that is declared const is guaranteed to remain constant for its lifetime, not throughout the entire execution of the program. For this reason, a const object cannot be used in constant expressions.

这第二句是说,没有const对象在常量表达式允许的,根本就没有,

它的原因是在第一个句子给出的,而我们可以预见哪里会可能使用const对象这样的情况下,在没有办法改变了明确的第二句话。

与学前禁止所有的狗没什么不同,因为更积极的可能会咬孩子。

C标准规定了这种行为,所以它不是真的应该对付IBM。他们的文件可能是模糊的(在一些读数),但他们是做正确的事情。

在(C99)6.8.4.2 The switch statement /3,我们看到:

The expression of each case label shall be an integer constant expression and no two of the case constant expressions in the same switch statement shall have the same value after conversion.

而在这同一标准的6.6 Constant expressions /6,它定义整型常量表达式这样的:

An integer constant expression shall have integer type and shall only have operands that are integer constants, enumeration constants, character constants, sizeof expressions whose results are integer constants, and floating constants that are the immediate operands of casts. Cast operators in an integer constant expression shall only convert arithmetic types to integer types, except as part of an operand to the sizeof operator.

正如你所看到的,也没有提及的const变量。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值