Search code examples
phpc++zeroarithmetic-expressions

Why is 02000 evaluated to 1024


Out of curiosity, I wrote a program like this:

auto number1 = 100;
auto number2 = 02000;
auto number3 = 2;

auto result = (number1 + number2) / number3;

std::cout << result;

Interestingly, the program outputs 562. So, in visual studio, i hovered over the variable "number2" and it showed (int) 1024. I didn't understand why this happened. So, I tried its equivalent in php which was this:

$number1 = 100;
$number2 = 02000;
$number3 = 2;

$result = ($number1 + $number2) / $number3;

echo $result;

The result was the same 562. What is it that I am missing because if I remove the zero in front of number2 to make it '2000', it shows 1050 as expected


Solution

  • Hey Hemil in PLs number system convention if you use 'zero' as a prefix, number will be considered as octal number but compiler will convert it into decimal number system and gives output in decimal. 02000 is in octal and its decimal value will be 1024.