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.16666667

Format Specifiers

%ccharacter
%ddecimal (integer) number (base 10)
%eexponential floating-point number
%ffloating-point number
%iinteger (base 10)
%ooctal number (base 8)
%sa string of characters
%uunsigned decimal (integer) number
%xnumber in hexadecimal (base 16)
%%print a percent sign
/%print a percent sign

Special Characters

\aaudible alert
\bbackspace
\fform feed
\nnewline, or linefeed
\rcarriage return
\ttab
\vvertical tab
\\backslash

Formatting Integers

At least five wideprintf("%5d", 10);'___10'
At least five wide, left justifiedprintf("%-5d", 10);'10___'
At least five wide, zero filledprintf("%05d", 10);'00010'
At least five wide, with a plus signprintf("%+5d", 10);'__+10'
Five-wide, plus sign, left justifiedprintf("%-+5d", 10);'+10___'

Formatting Strings

A simple stringprintf("%s", "Hello");'Hello'
A string with a minimum lengthprintf("%10s", "Hello");'_____Hello'
Minimum length, left-justifiedprintf("%-10s", "Hello");'Hello_____'

Formatting Floating Point Numbers

Print one position after the decimalprintf("%.1f", 10.3456);'10.3'
Two positions after the decimalprintf("%.2f", 10.3456);'10.35'
Eight-wide, two positions after the decimalprintf("%8.2f", 10.3456);'___10.35'
Eight-wide, four positions after the decimalprintf("%8.4f", 10.3456);'_10.3456'
Eight-wide, two positions after the decimal, zero-filledprintf("%08.2f", 10.3456);'00010.35'
Eight-wide, two positions after the decimal, left-justifiedprintf("%-8.2f", 10.3456);'10.35___'
Printing a much larger number with that same formatprintf("-8.2f", 101234567.3456);'101234567.35'