问题
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