c++csyntaxnumbers

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.


Solution

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

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