A Printf Format Reference Page (Cheat Sheet)
A Printf Format Reference Page (Cheat Sheet)
A printf formatting cheat sheet: I've created a printf format specifier cheat sheet, and thought I
would share it here (i.e., a "printf reference" page, or "cheat sheet").
A cool thing about the printf formatting syntax is that the specifiers you can use are very similar,
if not identical, between several different languages, including C, C++, Java, Perl, and Ruby, so
your knowledge is reusable, which I like..
%c Character
%d decimal (integer) number (base 10)
%e exponential floating-point number
%f floating-point number
%i integer (base 10)
%o octal number (base 8)
%s a string of characters
%u unsigned decimal (integer) number
%x number in hexadecimal (base 16)
%% print a percent sign
\% print a percent sign
printf("%3d", 0); 0
printf("%3d", 123456789); 123456789
printf("%3d", -10); -10
printf("%3d", -123456789); -123456789
printf("%-3d", 0); 0
printf("%-3d", 123456789); 123456789
printf("%-3d", -10); -10
printf("%-3d", -123456789); -123456789
\a audible alert
\b backspace
\f form feed
\n newline, or linefeed
\r carriage return
\t tab
\v vertical tab
\\ backslash
As you can see from that last example, because the backslash character itself is treated specially,
you have to print two backslash characters in a row to get one backslash character to appear in
your output.