Why is the initialization of an integer in VC ++ to 010 different from the initialization to 10?

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

I found this problem in VS2008.

+4
source share
4 answers

Because he interprets 010 as an octal number . And in the base-8 system, the number 10 is equal to the number 8 in the base-10 system (our standard counting system).

In general, in the C ++ world, the prefix of an integer literal with 0 indicates an octal literal, so the compiler behaves exactly as expected.

+7
source

In C , C++ , Objective C and related languages, the prefix 0 denotes the constant octal , so 010 = 8 in decimal.

+6
source

Keeping 0 in 010 means that this number is in octal form. So 010 means 8 in decimal.

+6
source

0 before the number will mean it in octal notation. Since octal uses base 8 , 010 will be 8 .

In the same way, 0x is used for hexadecimal notation using base 16 . So 0x10 will be 16 in decimal.

+6
source

All Articles