Skip to content

Instantly share code, notes, and snippets.

@7etsuo
Created September 12, 2024 08:48
Show Gist options
  • Save 7etsuo/91e6a23f330c4ddddae23d23ec7c95ee to your computer and use it in GitHub Desktop.
Save 7etsuo/91e6a23f330c4ddddae23d23ec7c95ee to your computer and use it in GitHub Desktop.
printf cheatsheet
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────┬──────────────────────────────────────────────────────────────────────────
printf Format Specifiers
│ ┌──────────┬──────────────────────────────────────────────────┬───────────────────────────────────────┐ │/** printf format specifiers
│ │ Specifier│Description │ Example Output │ │ * ███ ▄████████ ███ ▄████████ ███ █▄ ▄██████▄
│ ├──────────┼──────────────────────────────────────────────────┼───────────────────────────────────────┤ │ *▀█████████▄ ███ ███ ▀█████████▄ ███ ███ ███ ███ ███ ███
│ │ %d │Signed decimal integer │ printf("%d", 42); // 42 │ │ * ▀███▀▀██ ███ █▀ ▀███▀▀██ ███ █▀ ███ ███ ███ ███
│ │ %i │Signed decimal integer (same as %d) │ printf("%i", 42); // 42 │ │ * ███ ▀ ▄███▄▄▄ ███ ▀ ███ ███ ███ ███ ███
│ │ %u │Unsigned decimal integer │ printf("%u", 42); // 42 │ │ * ███ ▀▀███▀▀▀ ███ ▀███████████ ███ ███ ███ ███
│ │ %x │Unsigned hexadecimal integer (lowercas) │ printf("%x", 42); // 2a │ │ * ███ ███ █▄ ███ ███ ███ ███ ███ ███
│ │ %X │Unsigned hexadecimal integer (uppercas) │ printf("%X", 42); // 2A │ │ * ███ ███ ███ ███ ▄█ ███ ███ ███ ███ ███
│ │ %o │Unsigned octal integer │ printf("%o", 42); // 52 │ │ * ▄████▀ ██████████ ▄████▀ ▄████████▀ ████████▀ ▀██████▀
│ │ %f │Decimal floating-point │ printf("%f", 3.14159); // 3.141590 │ │ *
│ │ %e │Scientific notation (lowercase) │ printf("%e", 314.159); // 3.141590e+02│ │ * https://www.x.com/7etsuo
│ │ %E │Scientific notation (uppercase) │ printf("%E", 314.159); // 3.141590E+02│ │ */
│ │ %gShortest representation (%f or %e) │ printf("%g", 3.14); // 3.14 │ │
│ │ %GShortest representation (%f or %E) │ printf("%G", 3.14); // 3.14 │ │ ''---. .-. .--,;._
│ │ %cSingle characterprintf("%c", 'A'); // A │ │ ' `-\ \-' ./|\\ `'-.
│ │ %sString of charactersprintf("%s", "Hello"); // Hello │ │ .-----,| |--'/ | \\ \
│ │ %pPointer addressprintf("%p", ptr); // 0x7ffee4b78e0c │ │ / | | | / | \\ /
│ │ %% │Literal % characterprintf("%%"); // % │ │ | | | / | \`'`
│ └──────────┴──────────────────────────────────────────────────┴───────────────────────────────────────┘ │ ) | | | /\ / ,,
Flags │ / ;| | `-' `'` \`-.
│ ┌──────────┬──────────────────────────────────────────────────┬───────────────────────────────────────┐ │ / / | | ,' | \
│ │FlagDescriptionExample Output │ │ \_.' / ; | .-'-.,',' | '.
│ ├──────────┼──────────────────────────────────────────────────┼───────────────────────────────────────┤ │ ,_.' ,-' ; ) )/| | |
│ │-Left-justify within the given field widthprintf("%-10d", 42); // '42 ' │ │ ,' \ __ ,'' ''`. _| |--,.
│ │+Forces a sign (+ or -) for numbersprintf("%+d", 42); // +42 │ │ / `. ,',./ .-.-. \.--. / _) _,'--. )
│ │0Pad with leading zerosprintf("%05d", 42); // 00042 │ │ | ---..`.._| | /__|__\ ,-. \ |`/ ( `--. _)
│ │ │Print a space before positive numbersprintf("% d", 42); // 42 │ │ \ `. '\_; ((o|o)) ,' | \<_, `'-, )
│ │# │Alternate form (useful with o, x, X, f, etc.) │ p intf("%#x", 42); // 0x2a │ │ `. \ ,-''.--'--.''-. _,' \ __. '.
│ └──────────┴──────────────────────────────────────────────────┴───────────────────────────────────────┘ │ `. | / \ / \ / ,' `._)
Width and Precision │ \ ,' | .' `-.-' '. | / /
│ ┌──────────┬──────────────────────────────────────────────────┬───────────────────────────────────────┐ │ / _, | | | | |../ ,'
│ │SyntaxDescriptionExample Output │ │ .' /'--'._,,_ \ \ | / .' /
│ ├──────────┼──────────────────────────────────────────────────┼───────────────────────────────────────┤ │ (_._ '''-._ `.\ `. | ,' ,' ___/
│ │%10dMinimum width of 10 charactersprintf("%10d", 42); // ' 42' │ │ `-. \ '. `'-'-'` ,'-'`
│ │%.2fPrecision for floating-point numbers (2 decimals) │ printf("%.2f", 3.14159); // 3.14 │ │ \ `. \ |`'-...-'`
│ │%10.2fMinimum width and precisionprintf("%10.2f", 3.14159); // ' 3.14' │ │ | ; \ | / /
│ └──────────┴──────────────────────────────────────────────────┴───────────────────────────────────────┘ │ | | | / ,' .' ____
Length Modifiers │ (_/.'-' ( `. ,' `'._
│ ┌──────────┬──────────────────────────────────────────────────┬───────────────────────────────────────┐ │ `. `-' --.. )
│ │ ModifierDescriptionExample Output │ │ `. .--. `\
│ ├───────── ┼──────────────────────────────────────────────────┼───────────────────────────────────────┤ │ `.__.-\ \ `\_)
│ │ hShort integerprintf("%hd", (short)42); // 42 │ │ ( /mx
│ │ lLong integerprintf("%ld", 42L); // 42 │ │ `
│ │ llLong long integerprintf("%lld", 42LL); // 42 │ │
│ │ zSize type (for size_t) │ printf("%zu", (size_t)42); // 42 │ │
│ │ LLong double (for %Lf) │ printf("%Lf", 3.14159L); // 3.141590 │ │
│ └──────────┴──────────────────────────────────────────────────┴───────────────────────────────────────┘ │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────┘
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment