C++ int with preceding 0 changes entire value

后端 未结 4 813
春和景丽
春和景丽 2020-11-30 15:49

I have this very strange problem where if I declare an int like so

int time = 0110;

and then display it to the console the value returned i

4条回答
  •  自闭症患者
    2020-11-30 16:07

    The compiler is interpreting the leading zero as an octal number. The octal value of "110" is 72 in decimal. There's no need for the leading zero if you're just storing an int value.

    You're trying to store "time" as it appears on a clock. That's actually more complicated than a simple int. You could store the number of minutes since midnight instead.

提交回复
热议问题