Printf

Several programming languages implement a printf function, to output a formatted string. It originated from the C programming language, where it has the following prototype:
   int printf (const char *template, ...) 
The string constant template provides a description of the output, with placeholders marked by "%" escape characters, to specify both the location and the type of output that is to be performed. The printf function returns the number of characters printed, or a negative value if there occurred an output error. 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. C provides a number of derivative functions to gain further leverage from the printf functionality.:
  • fprintf enables printf output to be written to a regular file.
  • sprintf prints to a string (char array) instead of standard output. As it stands, sprintf is unsafe, since it will print into a buffer and not check for overflows, one may need to check the length of what is printed, which may be impossible to ensure, for example, if the program is making use of incoming data without restrictions.
  • snprintf behaves like sprintf, but has a length argument, limiting the amount of characters snprintf will write (including the string terminator). snprintf returns -1 if this limit was reached.
  • vprintf, vfprintf, vsprintf, vsnprintf behave respectively to the functions not prefixed with a v and make use of variable argument lists.

printf format placeholders

Formatting is denoted by 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." 
Some of the most commonly used placeholders follow:
  • `%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.
  • `%f' : Print a floating-point number in normal (fixed-point) notation.
  • `%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.
  • `%s' : Print a character string.
  • `%%' : 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

 

<< PreviousWord BrowserNext >>
notes from a defeatist
content analysis
opinion leadership
bioeconomics
spiral of silence
uses and gratifications
priming
poise
yuzhno sakhalinsk
ray brown
mona simpson (novelist)
samson (magazine)
the duke of wellington's regiment
tur
76th regiment of foot
dick posthumus
rhetorical criticism
33rd regiment of foot
visual rhetoric
rssowl
jacob ben asher
tambala
japan air system
arba'ah turim
east germanic language
prachatice
cyclone (industry)
voronezh oblast
napoleon hill
soder airlines
saint croix island, maine
malinao
siguiriyas
trinity college (vermont)
crimean gothic language
kenneth m. dye
mylne farmer
torah im derech eretz
www virtual library
geoffrey deuel
michael ridpath
canadian council on smoking and health
piaget
santa rosa city