// In Java language
System.out.printf("The child jumped up and down %d times.", 3 );
// In C language
printf("The child jumped up and down %d times.", 3 );
The remainder of this page uses C examples for brevity. Note that the actual format specifier strings can be
used in many different languages exactly as shown.
Here’s a quick summary of the most commonly used printf format specifiers:
| %c | single character |
| %d | decimal number ie, integer a base 10 whole number like 3 |
| %f | floating-point number
ie, has a decimal point like 3.096 |
| %s | a string of characters |
The %3d specifier is used with integers, and means a minimum width of three spaces, which, by default, will be right-justified:
| printf("%3d", 0); | 0 |
| printf("%3d", 123456789); | 123456789 |
| printf("%3d", -10); | -10 |
| printf("%3d", -123456789); | -123456789 |
Several different options allow you to include a minimum width specification, left-justify, or include a plus sign for positive numbers.
| Description | Code | Result |
|---|---|---|
| At least five wide | printf("'%5d'", 10); | ' 10' |
| At least five-wide, left-justified | printf("'%-5d'", 10); | '10 ' |
| At least five-wide, with a plus sign | printf("'%+5d'", 10); | ' +10' |
| Five-wide, plus sign, left-justified | printf("'%-+5d'", 10); | '+10 ' |
Examples:
| Description | Code | Result |
|---|---|---|
| Print one position after the decimal | printf("'%.1f'", 10.3456); | '10.3' |
| Two positions after the decimal | printf("'%.2f'", 10.3456); | '10.35' |
| Eight-wide, two positions after the decimal | printf("'%8.2f'", 10.3456); | ' 10.35' |
| Eight-wide, four positions after the decimal | printf("'%8.4f'", 10.3456); | ' 10.3456' |
| Eight-wide, two positions after the decimal, left-justified | printf("'%-8.2f'", 10.3456); | '10.35 ' |
| Printing a much larger number with that same format | printf("'%-8.2f'", 101234567.3456); | '101234567.35' |
Examples:
| Description | Code | Result |
|---|---|---|
| A simple string | printf("'%s'", "Hello"); | 'Hello' |
| A string with a minimum length | printf("'%10s'", "Hello"); | ' Hello' |
| Minimum length, left-justified | printf("'%-10s'", "Hello"); | 'Hello ' |
The following character sequences have a special meaning when used as printf format specifiers:
| \n | newline |
| \t | tab |
| \\ | backslash |
| %% | percent |
As you can see from that last two examples, because the backslash character and the percent character themselves are treated specially, you have to print two backslash characters in a row to get one backslash character to appear or two percent characters in a row to get a single persent character to appear in your output.
Here are a few examples of how to use these special characters:
| Description | Code | Result |
|---|---|---|
| Insert a tab character in a string | printf("Hello\tworld"); | Hello world |
| Insert a newline character in a string | printf("Hello\nworld"); | Hello world |
| Typical use of the newline character | printf("Hello world\n"); | Hello world |
| backslash characters | printf("C:\\Windows\\System32\\"); | C:\Windows\System32\ |
| percent characters | printf("The exam score is 97%%"); | 97% |