Formatted I/0--Printf


You can use the C function printf() to print formatted data.

The function is defined as follows:

int printf(char *format, arg list...)

This function prints to stdout the list of arguments according to the specified format string, and returns the number of characters printed.

The format string has two types of objects:

printf ()

Conversion Character

How the corresponding argument is printed
c
as a character
i or d
as a decimal integer
o
as an octal number
x
as a hexadecimal number
u
as an unsigned integer
l
modify an integer so it is long, eg. %lu or %ld
ll
modify an integer so it is long long, eg. %llu or %lld
s
as a string
f
as a floating point number
e
as a floating point number in scientific notation
g
in the e-format or f-format, whichever is shorter
%
print a % character

between the % and format char, we can put:

Some examples:

printf("%c%c%c\n", 'a', 'b', 'c'); //note "\n" is the new line character 

abc

printf("%c%3c%5c\n", 'A', 'B', 'C');

A   B       C

printf("%-2.3f\n",17.23478);

17.235

printf("VAT=17.5%%\n);

VAT=17.5%

References: