What does the extra 0 before the int value mean?

Inspired by a confusing piece of code, I have a small question regarding assigning a value to an integer:

#include <iostream> #include <cstdio> int main() { int i = 0101; std::cout << i << "\n"; } 

And the way out was 65, and I have no idea where it came from 65? Any idea?

+5
source share
2 answers

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

+11
source
Lambert has already explained this. So let me tell you what else you can do.

You can write a hex integer:

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

Output:

 257 
0
source

Source: https://habr.com/ru/post/1335941/


All Articles