What does an extra 0 in front of an int value mean?

半城伤御伤魂 提交于 2019-11-30 15:54:49

问题


Inspiring from a obfuscated piece of code, I have a small question regarding to assign value to an integer:

#include <iostream>
#include <cstdio>

int main() {
    int i = 0101;
    std::cout << i << "\n";
}

And the output was 65, and I have no idea where 65 came from? Any idea?


回答1:


It specifies an octal (base-8) number: 0101 == 1 * (8 * 8) + 1 == 65.




回答2:


Lambert already explained that. So let me tell you what else you can do.

You can write hexadecimal integer:

int main() {
    int i = 0x101; //0x specifies this (i.e 101) is hexadecimal integer
    std::cout << i << "\n"; //prints 257 (1 * 16 * 16 + 1)
}

Output:

257


来源:https://stackoverflow.com/questions/4720947/what-does-an-extra-0-in-front-of-an-int-value-mean

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!