为什么在VC ++中初始化一个整数到010不同于初始化它到10? [英] why is initializing an integer in VC++ to 010 different from initialising it to 10?

查看:105
本文介绍了为什么在VC ++中初始化一个整数到010不同于初始化它到10?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当整数被初始化为int a = 010时,a实际上设置为8.
但是对于int a = 10,a设置为10.
任何人都可以告诉我为什么a不是int a = 010设置为10?
在VS2008我发现这个问题。

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? On VS2008 i found this issue.

推荐答案

因为它解释 010 作为八进制格式的数字。在base-8系统中,数字 10 等于base-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 em> octal literal ,因此编译器的行为与预期的完全一样。

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.

这篇关于为什么在VC ++中初始化一个整数到010不同于初始化它到10?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆