Width Specifier in C

Level 256 keystrokes

Prints 42 in a field 5 characters wide, first right-aligned with %5d and then left-aligned with %-5d; the bars show the padding spaces.

C code

// The number to format is 42 (2 digits)
1int n = 42;
// %5d fills a 5-character field, adding 3 spaces on the left: "| 42|"
2printf("|%5d|\n", n);
// The - flag left-aligns, so the 3 spaces go on the right: "|42 |"
3printf("|%-5d|\n", n);

Output

|   42|
|42   |
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C code and how often they appear.

  • "× 4
  • |× 4
  • ;× 3
  • ,× 2
  • (× 2
  • )× 2
  • \× 2
  • %× 2
  • -× 1
  • =× 1

More C examples at level 2

← C typing practice