- 2

what If i store 025 value to int

Int i =025; Cout<<i; Then why output of this code is 21?

19th Mar 2017, 5:08 PM
Tushar
Tushar - avatar
4 Antworten
+ 6
Because any integer value starting with zero is treated as octal literal. and on translation from octal 25 to decimal, we get 25 = 2×8^1 + 5×8^0 = 2×8 + 5×1 = 16 + 5 = 21
19th Mar 2017, 5:37 PM
देवेंद्र महाजन (Devender)
देवेंद्र महाजन (Devender) - avatar
0
Thanx devender.. But why octal? Also is there any other conventions like this
19th Mar 2017, 6:08 PM
Tushar
Tushar - avatar
0
Because it's also a very good thing to convert a decimal into octal without even writing the code for it
19th Mar 2017, 6:09 PM
Tushar
Tushar - avatar
0
yes it is interesting when u start assigning integers with 0 it expects a octal number. just like when u start with '0x' the compiler expects a hexadecimal number. you can try ro assign 078 but it will show an error as '8' is not a valid octal input. it will take 0 to 7 as input. same you can try to input 0xa7 to see if it takes and it will take it as a hexadecimal number.
19th Mar 2017, 9:01 PM
Abhishek Basak
Abhishek Basak - avatar