Several programming languages implement a printf function, to output a formatted string. It originated from the C programming language, where it has a prototype similar to the following:
int printf(const char *format, ...)
The string constant format provides a description of the output, with placeholders marked by "%" escape characters, to specify both the relative location and the type of output that the function should produce.
For example in C printf("Color %s, number1 %d, number2 %05d, hex %x, float %5.2f.\n", "red", 123456, 89, 255, 3.14); will print following line (including new-line character, \n): Color red, number1 123456, number2 00089, hex ff, float 3.14.
The printf function returns the number of characters printed, or a negative value if an output error occurred.
Perl also has a printf function. Common Lisp has a format function which acts according to the same principles as printf, but uses different characters for output conversion. Python has an analogue (as the % operator). The GLib library contains g_print, an implementation of printf. Some Unix systems have a printf program for use in shell scripts.
PHP also has the printf function, with the same specifications and usage as that in C/C++.
JavaScript does not have a printf function, despite it being a curly bracket programming language.
ANSI C provides a number of derivative functions to gain further leverage from the printf functionality:
int fprintf(FILE *stream, const char *format, ...)
fprintf enables printf output to be written to any file. Programmers most frequently use it to print errors, by writing to the standard error device, but it can also operate with any file opened with the fopen function.
int sprintf (char *str, const char *format, ...)
sprintf prints to a string (char array) instead of to standard output. Users of sprintf must ensure, via calculation or via a guard page, that the resulting string will not be larger than the memory allocated for str. Failure to ensure this will allow a buffer overflow to occur.
It is notable that in PHP the sprintf function does not have the str argument. Instead, it returns the formatted output string. The prototype in PHP is like this:
string sprintf (const string format, ...)
As an alternative, many environments offer the snprintf function:
int snprintf(char *str, size_t size, const char *format, ...)
snprintf is guaranteed not to write more than size bytes into str, so programmers using it can avoid any risk of a buffer overflow, as in the following code snippet:
#define BUFFER_SIZE 50 char buf*; int n; ... n = snprintf(buf, BUFFER_SIZE, "Your name is %s.\n", username); if (n < 0 || n >= BUFFER_SIZE) /* Handle error */
If username in the above example exceeds 50 characters in length, the function will limit the string that gets saved in buf by cutting off final characters (truncating). This may seem undesirable, yet preferable to having a security vulnerability, which buffer overflows can often lead to. Additionally, the return code of snprintf indicates how many characters the function would have written to the string had enough space existed. Systems can use this information to allocate a new (larger) buffer if they require the whole string.
snprintf does not form part of the widely implemented ANSI C standard, as sprintf does. However, it came into the language for the later C99 standard and often existed in C libraries before that.
Another safe sprintf alternative is asprintf:
int asprintf(char **ret, const char *format, ...)
asprintf automatically allocates enough memory to hold the final string. It sets *ret to a pointer to the resulting string, or NULL if not enough memory was available. The programmer using asprintf has the responsibility of freeing the allocated memory after use. Though not part of any standard, asprintf comes in the C libraries of several operating systems (including OpenBSD, FreeBSD, and NetBSD) and on other platforms in the libiberty library.
int vprintf(const char *format, va_list ap); int vfprintf(FILE *stream, const char *format, va_list ap); int vsprintf(char *str, const char *format, va_list ap); int vsnprintf(char *str, size_t size, const char *format, va_list ap); int vasprintf(char **ret, const char *format, va_list ap);
These are analogous to the above functions without the vs, except that they use variable argument lists. These functions offer the ability for programmers to essentially create their own printf variants. For instance, a programmer could write a function
void fatal_error(const char *format, ...)
which would use the va_start macro to obtain a va_list variable from the extra parameters, print a message on the standard error device using vfprintf, clean up after the va_list variable with the va_end macro, and finally perform the necessary tasks to cleanly shut down the program.
Another common application of these functions is to write a custom printf that prints to a different target than a file. For instance, a graphical library might provide a printf-like function with X and Y coordinates:
int graphical_printf(int x, int y, const char *format, ...)
This would work by temporarily saving the string to a private buffer using vsnprintf or vasprintf.
Formatting takes place via placeholders within the format string. For example, if a program wanted to print out a person's age, it could present the output by prefixing it with "Your age is ". To denote that we want the integer for the age to be shown immediately after that message, we may use the format string: "Your age is %d."
The syntax for a format placeholder is "%****type".
Where type can be any of:
scanf() function for input.
Flags can be omitted or be any of:
Width can be omitted or be any of:
.Precision can be omitted or be any of:
Length can be omitted or be any of:
If the syntax of a conversion specification is invalid, behavior remains undefined. If there are too few or extraneous 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. Note that some compilers, like the GNU Compiler Collection, will statically check the format strings of printf like functions and warn about problems.
Some applications (like the Apache HTTP Server) include their own printf-like function, and embed extensions into it. However these all tend to have the same problems that register_printf_function() has.
Most non-C languages that have a printf like function work around the lack of this feature by just using the "%s" format and converting the object to a string representation. C++ offers a notable exception, in that it has a printf function inherited from its C history but instead has a completely different mechanism that is preferred.