问题描述
当整数初始化为 int a = 010
时, a
实际上设置为8,但对于 int a = 10
, a
设置为10。
谁能告诉我为什么 int a = 010 > a 未设置为10?
When an integer is initialized as int a = 010
, a
is actually set to 8, but for int a = 10
, a
is set to 10.Can anyone tell me why a
is not set to 10 for int a = 010
?
推荐答案
因为它会将 010
解释为八进制格式的数字。在以8为底的系统中,数字 10
等于以10为底的数字 8
(我们的标准计数系统)。
Because it's interpreting 010
as a number in octal format. And in a base-8 system, the number 10
is equal to the number 8
in base-10 (our standard counting system).
在C ++的世界中,通常在整数文字前加上 0
来指定八进制文字,因此编译器的行为完全符合预期。
More generally, in the world of C++, prefixing an integer literal with 0
specifies an octal literal, so the compiler is behaving exactly as expected.
这篇关于为什么在C ++中将整数初始化为010与将其初始化为10不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!