what is the reason for the difference between setprecision (12) and setprecision (012) for example,in c++?

a 夏天 提交于 2019-12-10 23:03:15

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!