Tsallaka zuwa abun ciki
Justice Tech School

printf Formatting

Width, precision and flags control output: %5d, %-5d, %05d, %.3f, %x, %o and %%.

Example
#include <stdio.h>

int main(void) {
  printf("[%5d][%-5d][%05d]\n", 42, 42, 42);
  printf("%.3f %x %o %c %%\n", 3.14159, 255, 8, 65);
  return 0;
}

Result

[   42][42   ][00042]
3.142 ff 10 A %

Exercise

Zero pad width 5.

"%d"

Quiz Test

What does %x print?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.