0是十进制文字还是八进制文字?

本文翻译自:Is 0 a decimal literal or an octal literal?

Zero is always zero, so it doesn't matter. 零总是为零,所以没关系。 But in a recent discussion with my friend he said that octal literals are almost unused today. 但在最近与我的朋友讨论时,他说今天几乎没有使用八进制文字。 Then it dawned upon me that actually almost all integer literals in my code are octal, namely 0 . 然后我突然意识到我的代码几乎所有的整数文字都是八进制的,即0

Is 0 an octal literal according to the C++ grammar? 根据C ++语法, 0是八进制文字吗? What does the standard say? 标准说什么?


#1楼

参考:https://stackoom.com/question/Svq6/是十进制文字还是八进制文字


#2楼

Yes, 0 is an Octal literal in C++. 是的, 0是C ++中的八进制文字

As per the C++ Standard: 根据C ++标准:

2.14.2 Integer literals [lex.icon] 2.14.2整数文字[lex.icon]

integer-literal:  
    decimal-literal integer-suffixopt  
    octal-literal integer-suffixopt  
    hexadecimal-literal integer-suffixopt  
decimal-literal:  
    nonzero-digit  
    decimal-literal digit  
octal-literal:  
    0                           <--------------------<Here>
    octal-literal octal-digit

#3楼

Any integer value prefixed with 0 is an octal value. 前缀为0任何整数值都是八进制值。 Ie: 01 is octal 1, 010 is octal 10, which is decimal 8, and 0 is octal 0 (which is decimal, and any other, 0). 即:01是八进制1,010是八进制10,是十进制8,0是八进制0(十进制,任何其他,0)。

So yes, '0' is an octal. 所以是的,'0'是八进制。

That's plain English translation of the grammar snippet in @Als's answer :-) 这是@ Als答案中语法片段的简单英文翻译:-)


An integer prefixed with 0x is not prefixed with 0 . 前缀为0x的整数不带前缀0 0x is an explicitly different prefix. 0x是明确不同的前缀。 Apparently there are people who cannot make this distinction. 显然有些人无法做出这种区分。

As per that same standard, if we continue: 按照同样的标准,如果我们继续:

 integer-literal:
     decimal-literal integer-suffixopt
     octal-literal integer-suffixopt
     hexadecimal-literal integer-suffixopt
 decimal-literal:
     nonzero-digit                       <<<---- That's the case of no prefix.
     decimal-literal digit-separatoropt digit
 octal-literal:
     0                                    <<<---- '0' prefix defined here.
     octal-literal digit-separatoropt octal-digit <<<---- No 'x' or 'X' is
                                                          allowed here.
 hexadecimal-literal:
     0x hexadecimal-digit                 <<<---- '0x' prefix defined here
     0X hexadecimal-digit                 <<<---- And here.
     hexadecimal-literal digit-separatoropt hexadecimal-digit

#4楼

Apparently all integer literals starting with zero are in fact octal. 显然,所有以零开头的整数文字实际上是八进制的。 This means that it includes 0 as well. 这意味着它也包含0。 This makes little difference since zero is zero. 由于零为零,这几乎没有区别。 But not knowing this fact can hurt you. 但不知道这个事实会伤害你。

I realized this when I was trying to write a program to convert binary numbers to decimal and hexidecimal output. 当我试图编写一个程序来将二进制数转换为十进制和十六进制输出时,我意识到了这一点。 Everytime that I was giving a number starting with zero I was getting the wrong output (For example, 012 = 10, not 12). 每当我给出一个以零开头的数字时,我得到的输出错误(例如,012 = 10,而不是12)。

It's good to know this information so you don't make the same mistake. 知道这些信息是很好的,这样你就不会犯同样的错误。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值