开发者

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

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

#include <iostream>
#开发者_StackOverflow社区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?


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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜