A printf format reference page (cheat sheet)
By Alvin Alexander. Original site: http://alvinalexander.com/programming/printf-format-cheat-sheet
- printf formatting with Perl and Java
- printf format specifiers - summary
- Controlling printf integer width
- Left-justifying printf integer output
- The printf zero-fill option
- printf integer formatting
- printf - floating point numbers
- printf string formatting
- Summary of special printf characters
- Related printf content
Summary: This page is a printf formatting cheat sheet. I originally created this cheat sheet for my own purposes, and then thought I would share it here.
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, Ruby, and others, so your knowledge is reusable, which is a good thing.printf formatting with Perl and Java
In this cheat sheet I'm going to show all the examples using Perl, but at first it might help to see one example using both Perl and Java. So, here's a simple Perl printf example to get us started:
And here are three different Java printf examples, using different methods that are available to you in the Java programming language:
As you can see in that last String.format example, that line of code doesn't print any output, while the first line prints to standard output, and the second line prints to standard error.
In the remainder of this document I'm going to use Perl examples, but again, the actual format specifier strings can be used in many different languages.
printf format specifiers - summary
Here's a quick summary of the available printf format specifiers:
Controlling printf integer width
The "%3d" specifier means a minimum width of three spaces, which, by default, will be right-justified. (Note: the alignment is not currently being displayed properly here.)
Left-justifying printf integer output
To left-justify those previous printf examples, just add a minus sign (
-
) after the%
symbol, like this:The printf zero-fill option
To zero-fill your printf integer output, just add a zero (
0
) after the%
symbol, like this:printf integer formatting
Here is a collection of printf examples for integer printing. Several different options are shown, including a minimum width specification, left-justified, zero-filled, and also a plus sign for positive numbers.
printf - floating point numbers
Here are several examples showing how to print floating-point numbers with printf.
printf string formatting
Here are several printf formatting examples that show how to format string output with
printf
format specifiers.Summary of special printf characters
The following character sequences have a special meaning when used as
printf
format specifiers: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.
Here are a few examples of how to use this special characters:
world