tags:

views:

96

answers:

3

In c++ when you write setprecision (12) for example, 12 is in the base of 10 but when you write it like setprecision (012) it is an octal number,why?

+12  A: 

Because constants with leading zeros (other than leading 0x) are always octal:

An octal integer literal (base eight) begins with the digit 0 and consists of a sequence of octal digits.

C++ draft standard (n1905) §2.13.1

It has nothing at all to do with setprecision.

Matthew Flaschen
thanks for the sufficient explanation.
Ehsan Mamakani
@Ehsan: click the checkmark next to the answer to officially say that.
Potatoswatter
already did it man!
Ehsan Mamakani
+3  A: 

In C++, if an integer literal begins with a 0 digit (and that 0 is not followed by a x), the following digits are treated as octal digits.

strager
+4  A: 

Because that's how it worked in C. Back when C was designed, octal numbers were in frequent use, so they put in a notation for them. Currently, it's rarely helpful and mostly confusing.

David Thornley