What does it mean when a numeric constant in C/C++ is prefixed with a 0?

Ok... So I had a silly idea and tried putting the value 0123 into an int, just curious to see what would happen, I assumed that when I printed the value I'd get 123, but instead I got 83... Any ideas why? what happens inside the compiler/memory that makes this value become 83?

I tried this in C++ and C with GCC compiler and also tried with a float which yielded the same results.


In C/C++ a numeric literal prefixed with a '0' is octal (base 8).

See http://www.cplusplus.com/doc/tutorial/constants/


Congratulations! You've discovered octal.


This is because any number starting with 0 like this is considered to be in octal (base 8) not decimal.

Same thing if you start with 0x you will get hexadecimal


The leading 0 indicates an "octal" number. So it becomes 1*8^2 + 2*8^1 + 3*8^0 = 83