Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is initializing an integer in C++ to 010 different from initializing it to 10?

Tags:

c++

When an integer is initialized as int a = 010, a is actually set to 8, but for int a = 10, a is set to 10. Can anyone tell me why a is not set to 10 for int a = 010?

like image 963
Lakshmi Avatar asked Jun 15 '11 14:06

Lakshmi


1 Answers

Because it's interpreting 010 as a number in octal format. And in a base-8 system, the number 10 is equal to the number 8 in base-10 (our standard counting system).

More generally, in the world of C++, prefixing an integer literal with 0 specifies an octal literal, so the compiler is behaving exactly as expected.

like image 113
Cody Gray Avatar answered Sep 20 '22 10:09

Cody Gray