The output function printf translates internal values to characters.
int printf(char *format, arg1, arg2, ...)
printf converts, formats and prints its arguments on the standard output under the control of the format. It returns the number of characters printed.
The format string contains two types of objects:
- Ordinary characters: Copied to the output stream
- Conversion specifications: Converts and prints the next argument passed to printf. They begin with a % and end with a conversion character. Between % and the conversion character, there may be:
- – (minus sign): specifies left adjustment of the converted argument.
- a number: specifies the minimum field width. The converted argument will be printed in a field at least this wide.
- . (dot): Separates the field width from the precision.
- a number (precision): specifies the maximum number of characters to be printed from a string, or the number of digits after decimal point of a floating-point value, or the minimum number of digits for an integer.
- An h if the integer is to be printed as a short, or l if it is to be printed as a long.
Conversion characters:
Character | Argument Type / Printed As |
---|---|
d, i | int; decimal number |
o | int; unsigned octal number |
x, X | int; unsigned hexadecimal number |
u | int; unsigned decimal number |
c | int; single character |
s | char *; print characters from the string until the null character('\0') or the number of characters given by the precision. |
f | float; [-]m.dddddd, where the number of d's is given by the precision(default is 6). |
e, E | double; [-]m.dddddd e ± xx or [-]m.dddddd E ± xx, where the number of d's is given by the precision (default is 6). |
g, G | double; |
p | void *; pointer |
% | print a %; no argument is converted |
A width or precision may be specified as *, in which case the value is computed by converting the next argument (which must be an int). For example:
printf("%.*s", width, str);
prints at most width characters from the string str.
Let’s look at the following code:
#include <stdio.h> int main() { char *str = "Hello World!"; printf("[%s]\n", str); printf("[%10s]\n", str); printf("[%.10s]\n", str); printf("[%-10s]\n", str); printf("[%15s]\n", str); printf("[%.15s]\n", str); printf("[%-15s]\n", str); printf("[%15.10s]\n", str); printf("[%-15.10s]\n", str); return 0; }
Here is its output:
[Hello World!] [Hello World!] [Hello Worl] [Hello World!] [ Hello World!] [Hello World!] [Hello World! ] [ Hello Worl] [Hello Worl ]
printf decides the number and types of arguments using its first argument, so you should be careful with it.
In the following example:
char *s = "s%s"; printf("%sn", s); /* Safe to use */ printf(s); /* Erroneous */
String s contains a ‘%’ character, so directly passing it to printf is erroneous.
sprintf is very similar to printf, except the fact that it stores the output in a string.
int sprintf(char *string, char *format, arg1, arg2, ...)
sprintf uses format argument to format arg1, arg2, etc. and places the result in string. string must be big enough to store the result.