为什么这句话int a = 012945;在C ++中给我一个错误,如“无效的八进制数字” ? [英] Why this statement int a= 012945; in C++ gives me an error like "invalid octal digit" ?

查看:321
本文介绍了为什么这句话int a = 012945;在C ++中给我一个错误,如“无效的八进制数字” ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用2017年的视觉工作室,所以是因为它给我一个错误无效的八进制数字?



我有什么尝试过:



如果我给出一个以0开头的整数变量值,那么只有这样的错误才会发生这样的事情:e = 0129612;

但是如果我在第一个地方不使用0就不会给出任何错误。

I'm using visual studio 2017,so is it because of that it gives me an error "invalid octal digit"?

What I have tried:

if i give a integer variable value that starts with 0 then only this error comes something like this int e = 0129612;
but it doesn't gives any error if i just don't use 0 on the first place.

推荐答案

八进制数字的范围可以是0到7这是因为八进制是基数8.以0开头的文字整数值被认为是八进制。如果以0x开头,则为十六进制值。
Octal digits can range from 0 to 7. This is because octal is base 8. A literal integer value starting with 0 is considered to be octal. If it starts with 0x it is a hexadecimal value.


C / C ++知道三种类型的整数文字:八进制,十进制和十六进制。

自C + +14有第四种类型:二进制。



一个以零开头,后跟另一个数字的文字被视为八进制值(基数为8),这是不允许的包含数字8和9.



另请参见 integer literal - cppreference.com [ ^ ]。
C/C++ knows three types of integer literals: octal, decimal, and hex.
Since C++14 there is a fourth type: binary.

A literal beginning with a zero and followed by another digit is treated as octal value (base 8) which is not allowed to contain the digits 8 and 9.

See also integer literal - cppreference.com[^].


引用:

如果我给出一个以0开头的整数变量值,那么只有这个错误来自这样的int e = 0129612;

但如果我不在第一个地方使用0,它不会给出任何错误。

if i give a integer variable value that starts with 0 then only this error comes something like this int e = 0129612;
but it doesn't gives any error if i just don't use 0 on the first place.



因为它是原则:从0开始的整数是八进制(基数为8)


Because it is the principle: integers starting with 0 are octal (base 8)


这篇关于为什么这句话int a = 012945;在C ++中给我一个错误,如“无效的八进制数字” ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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