Example 1 — Formatting a small report
Width, precision, and flags produce aligned columns.
#include <stdio.h>
int main(void) {
printf("%-8s|%6s\n", "Item", "Price");
printf("%-8s|%6.2f\n", "Pen", 1.5);
printf("%-8s|%6.2f\n", "Notebook", 12.0);
printf("id=%05d hex=%x char=%c\n", 42, 255, 'Z');
printf("100%%\n");
return 0;
} How It Works
%-8sleft-aligns a string in 8 columns.%6.2fuses width 6 and 2 decimals.%05dpads with zeros;%xprints hexadecimal.%%prints one percent sign.
