Without width printing format in C
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:
Is this good programming practice?;
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...
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.
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.)
精彩评论