From Academic Kids

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. Python has a form of one (as the % operator). 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


Academic Kids Menu

  • Art and Cultures
    • Art (
    • Architecture (
    • Cultures (
    • Music (
    • Musical Instruments (
  • Biographies (
  • Clipart (
  • Geography (
    • Countries of the World (
    • Maps (
    • Flags (
    • Continents (
  • History (
    • Ancient Civilizations (
    • Industrial Revolution (
    • Middle Ages (
    • Prehistory (
    • Renaissance (
    • Timelines (
    • United States (
    • Wars (
    • World History (
  • Human Body (
  • Mathematics (
  • Reference (
  • Science (
    • Animals (
    • Aviation (
    • Dinosaurs (
    • Earth (
    • Inventions (
    • Physical Science (
    • Plants (
    • Scientists (
  • Social Studies (
    • Anthropology (
    • Economics (
    • Government (
    • Religion (
    • Holidays (
  • Space and Astronomy
    • Solar System (
    • Planets (
  • Sports (
  • Timelines (
  • Weather (
  • US States (


  • Home Page (
  • Contact Us (

  • Clip Art (
Personal tools