开发者

Without width printing format in C

开发者 https://www.devze.com 2022-12-26 01:22 出处:网络
in C if I have a printf statement containing say \"%.2f\", it 开发者_运维知识库says that the precision is 2 digits after the decimal place. I haven\'t explicitly specify the width. I have two question

in C if I have a printf statement containing say "%.2f", it 开发者_运维知识库says that the precision is 2 digits after the decimal place. I haven't explicitly specify the width. I have two questions:

  1. Is this good programming practice?;

  2. Is without specifying the width means that the width of the field will get adjusted automatically when printing the number, irrespective of the number of digits it contains?

Thanks a lot...


  1. If this does what you want to do, then it's good practice. If it does something that you don't want to do, it's bad practice. Simple as that.

  2. Yes, the width is adjusted to fit the number. (Note that the width, if you specify it, is a minimum only; if you specified a width and the number wouldn't fit, the result would become wider than you might have expected.)

0

精彩评论

暂无评论...
验证码 换一张
取 消