Printing leading 0's in C?

前端 未结 10 1383
[愿得一人]
[愿得一人] 2020-11-22 17:26

I\'m trying to find a good way to print leading 0\'s, such as 01001 for a zipcode. While the number would be stored as 1001, what is a good way to do it?

I thought

10条回答
  •  萌比男神i
    2020-11-22 17:57

    If you are on a *NIX Machine:

    man 3 printf
    

    This will show a manual page, similar to:

    0 The value should be zero padded. For d, i, o, u, x, X, a, A, e, E, f, F, g, and G conversions, the converted value is padded on the left with zeros rather than blanks. If the 0 and - flags both appear, the 0 flag is ignored. If a precision is given with a numeric conversion (d, i, o, u, x, and X), the 0 flag is ignored. For other conversions, the behavior is undefined.

    Even though the question is for C, this page may be of aid.

提交回复
热议问题