为什么 int 变量开头的零会出错?

Why zero at the beginning of int variable gives error?

#include <iostream>
using namespace std;
int main()
{
    struct information
    {
        string  name;
        string  bloodgroup;
        int     mobno;
    };

    information person1={"Ali Hamza","O-",434233434};
    information person2={"Akram Ali","B",034};
    cout << endl << person1.name << endl;
    cout << person1.bloodgroup << endl << person1.mobno<< endl;
    cout << endl << person2.name << endl << person2.bloodgroup << endl << person2.mobno<<endl;

    int num = 09;
    cout << num;

    return 0;
}

我想知道我什么时候看到像 invalid digit in octal constant 9 和 8.Also 这样的错误,如果零是第一个数字,它会为 "mobno"(在结构中)的值打印错误的值,但是它当 program.Is 末尾的 num 的第一个数字为零时出错,有人会为我解释一下吗?

在 C/C++ 中以 0 开头的整数文字意味着您打算将其解释为 octal 或 base-8。

因此,例如,数字“034”将被解释为 3*8^1 + 4*8^0 = 3*8 + 4*1 = 28。因此它等同于“28” .

整数文字“09”无效,因为“9”不是八进制数字。

2.14.2 整数文字

An integer literal may have a prefix that specifies its base and a suffix that specifies its type. [...] An octal integer literal (base eight) begins with the digit 0 and consists of a sequence of octal digits.

八进制加起来是:

01, 02, 03, 04, 05, 06, 07, 10

因此,编译器会接受 89 作为八进制数的一部分。