C programming: Printing leading 0's in C
Print with padding by Zeros in C.
The printf() statement can be used with the following format specifier to print with padding by zeros.
- %d : No padding
- %01d : 1 digit padding with zeros
- %02d : 2 digits padding with zeros
- %03d : 3 digits padding with zeros
- %04d : 4 digits padding with zeros
- %05d : 5 digits padding with zeros
Code:
# include <stdio.h>
int main ()
{
int x = 123;
printf ("2 digits padding: %02d\n\n", x);
printf ("3 digits padding: %03d\n\n", x);
printf ("4 digits padding: %04d\n\n", x);
printf ("5 digits padding: %05d\n\n", x);
printf ("6 digits padding: %06d\n\n", x);
return 0;
}
Output:
2 digits padding: 123 3 digits padding: 123 4 digits padding: 0123 5 digits padding: 00123 6 digits padding: 000123
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/c-programming-exercises/c-snippets/printing-with-leading-0s-in-c.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics