variables - C++ int with preceding 0 changes entire value -


i have strange problem if declare int so

int time = 0110; 

and display console value returned 72. when remove 0 @ front int time = 110; console displays 110 expected.

two things i'd know, first of why preceding 0 @ start of int , there way stop 0110 @ least equals 110?
secondly there way keep 0110 returns 0110?
if take crack guess @ variable name i'm trying operations 24hr time, @ point time before 1000 causing problems because of this.

thanks in advance!

an integer literal starts 0 defines octal integer literal. in c++ there 4 categories of integer literals

integer-literal:     decimal-literal integer-suffixopt     octal-literal integer-suffixopt     hexadecimal-literal integer-suffixopt     binary-literal integer-suffixopt 

and octal-integer literal defined following way

octal-literal:     0 octal-literal     opt octal-digit 

that starts 0.

thus octal integer literal

0110 

corresponds following decimal number

8^2 + 8^1  

that equal 72.

you can sure 72 in octal representation equivalent 110 running following simple program

#include <iostream> #include <iomanip>  int main()  {     std::cout << std::oct << 72 << std::endl;      return 0; } 

the output is

110 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -