Zero Padding in C with printf#
In C, printf() can add leading zeros to make a number occupy a fixed minimum width.
The syntax is:
| |
For example:
| |
Output:
| |
Here:
0→ pad with zeros6→ minimum field width is 6 charactersd→ print a signed decimal integer
Zero padding vs space padding#
| |
Output:
| |
while:
| |
Output:
| |
So:
| |
Example: hexadecimal#
| |
For:
| |
the output is:
| |
%08lx means:
0→ zero padding8→ minimum width of 8 charactersl→longx→ hexadecimal
Zero padding is useful when displaying IDs, hexadecimal values, timestamps, counters, or other fixed-width numbers where aligned output is desirable.