问题
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?
回答1:
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.
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/2740715/what-is-the-reason-for-the-difference-between-setprecision-12-and-setprecision