|
In the C programming language, the ubiquitous printf function prints the optional arguments under the control of the template string template to the stream stdout. It performs basic formatting (hence the f) such as conversion of numerical types to character strings. It returns the number of characters printed, or a negative value if there occurred an output error.
int printf (const char *template, ...)
Perl also has a printf function. Common Lisp has a format function which acts under the same principles as printf, but uses different characters for output conversion. The GLib library contains g_printf, an implementation of printf. Some Unix systems have a printf program for use in shell scripts.
There are many very similar derivative C functions, including:
fprintf -- prints to a specified file pointer instead of just stdout
sprintf -- prints to a string (char array) instead of a stream
snprintf -- prints to a string (char array) instead of a stream, but checks output length
vprintf, vfprintf, vsprintf, vsnprintf -- as the non v- versions with a different argment passing method (varargs)
Output Conversions
Here is a table summarizing what all the different conversions do (in the C programming language, other versions might be different):
- `%d', `%i'
- Print an integer as a signed decimal number. `%d' and `%i' are synonymous for output, but are different when used with
scanf() function for input.
- `%o'
- Print an integer as an unsigned octal number.
- `%u'
- Print an integer as an unsigned decimal number.
- `%x', `%X'
- Print an integer as an unsigned hexadecimal number. `%x' uses lower-case letters and `%X' uses upper-case.
- `%f'
- Print a floating-point number in normal (fixed-point) notation.
- `%e', `%E'
- Print a floating-point number in exponential notation. `%e' uses lower-case letters and `%E' uses upper-case.
- `%g', `%G'
- Print a floating-point number in either normal or exponential notation, whichever is more appropriate for its magnitude. `%g' uses lower-case letters and `%G' uses upper-case.
- `%a', `%A'
- Print a floating-point number in a hexadecimal fractional notation which the exponent to base 2 represented in decimal digits. `%a' uses lower-case letters and `%A' uses upper-case.
- `%c'
- Print a single character.
- `%lc'
- Print a single wide character.
- `%s'
- Print a character string.
- `%ls'
- Print a wide character string.
- `%p'
- Print the value of a pointer.
- `%n'
- Get the number of characters printed so far. Note that this conversion specification never produces any output.
- `%%'
- Print a literal `%' character.
If the syntax of a conversion specification is invalid, behavior is undefined. If there are too few or extranous function arguments provided to supply values for all the conversion specifications in the template string, or if the arguments are not of the correct types, the results are also undefined. In a number of cases the undefined behavior has lead to "Format string attack" security vulnerabilities.
See also
|