printf
Usage
printf("Hello Everyone! \n");
printf("the meaning of life and everything is %d \n", 42);
printf("my name is %s \n", "tom");
printf("the retirement age is %f years", 66.0 + 2.0/12.0);Hello Everyone!
the meaning of life and everything is 42
my name is tom
the retirement age is 66.16666667Format Specifiers
| %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 |
Special Characters
| \a | audible alert |
| \b | backspace |
| \f | form feed |
| \n | newline, or linefeed |
| \r | carriage return |
| \t | tab |
| \v | vertical tab |
| \\ | backslash |
Formatting Integers
| At least five wide | printf("%5d", 10); | '___10' |
| At least five wide, left justified | printf("%-5d", 10); | '10___' |
| At least five wide, zero filled | printf("%05d", 10); | '00010' |
| At least five wide, with a plus sign | printf("%+5d", 10); | '__+10' |
| Five-wide, plus sign, left justified | printf("%-+5d", 10); | '+10___' |
Formatting Strings
| 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_____' |
Formatting Floating Point Numbers
| 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, zero-filled | printf("%08.2f", 10.3456); | '00010.35' |
| 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' |