This articleneeds additional citations forverification. Please helpimprove this article byadding citations to reliable sources. Unsourced material may be challenged and removed. Find sources: "Printf" – news ·newspapers ·books ·scholar ·JSTOR(February 2015) (Learn how and when to remove this message) |

printf is aC standard libraryfunction thatformatstext and writes it tostandard output. The function accepts a formatc-stringargument and avariable number of value arguments that the functionserializes per the format string. Mismatch between the format specifiers and count andtype of values results inundefined behavior and possibly programcrash or othervulnerability.
The format string isencoded as atemplate language consisting of verbatim text andformat specifiers that each specify how to serialize a value. As the format string is processed left-to-right, a subsequent value is used for each format specifier found. A format specifier starts with a% character and has one or more following characters that specify how to serialize a value.
The standard library provides other, similar functions that form a family ofprintf-like functions. The functions share the same formatting capabilities but provide different behavior such as output to a different destination or safety measures that limit exposure to vulnerabilities. Functions of the printf-family have been implemented in othercomputer programming contexts (i.e.,programming languages) with the same or similarsyntax andsemantics.
Thescanf() C standard library function complements printf by providing formatted input (a.k.a.lexing, a.k.a.parsing) via a similar format string syntax.
The name,printf, is short forprint formatted whereprint refers to output to aprinter although the function is not limited to printer output. Today, print refers to output to any text-based environment such as aterminal or afile.
Early programming languages likeFortran used special statements with different syntax from other calculations to build formatting descriptions.[1] In this example, the format is specified on line601, and thePRINT[a] command refers to it by line number:
PRINT601,IA,IB,AREA 601FORMAT(4HA=,I5,5HB=,I5,8HAREA=,F10.2,13HSQUAREUNITS)
Hereby:
4H indicates astring of 4characters" A= " (H meansHollerith Field);I5 indicates aninteger field of width 5;F10.2 indicates afloating-point field of width 10 with 2 digits after the decimal point.An output with input arguments100,200, and1500.25 might look like this:
A= 100 B= 200 AREA= 1500.25 SQUARE UNITSIn 1967,BCPL appeared.[2] Its library included thewritef routine.[3] An example application looks like this:
WRITEF("%I2-QUEENS PROBLEM HAS %I5 SOLUTIONS*N", NUMQUEENS, COUNT)Hereby:
%I2 indicates aninteger of width 2 (the order of the format specification's field width and type is reversed compared to C'sprintf);%I5 indicates an integer of width 5;*N is a BCPLlanguageescape sequence representing anewline character (for which C uses the escape sequence\n).In 1968,ALGOL 68 had a more function-likeAPI, but still used special syntax (the$ delimiters surround special formatting syntax):
printf(($"Color "g", number1 "6d,", number2 "4zd,", hex "16r2d,", float "-d.2d,", unsigned value"-3d"."l$,"red",123456,89,BIN255,3.14,250));
In contrast to Fortran, using normal function calls and data types simplifies the language andcompiler, and allows the implementation of theinput/output (I/O) to be written in the same language.
These advantages were thought to outweigh the disadvantages (such as notype safety in many instances) up until the 2000s, and in most newer languages of that era I/O is not part of the syntax.
People have since learned[4] that this potentially results in consequences, ranging from security exploits to hardware failures (e.g., phone's networking capabilities being permanently disabled after trying to connect to an access point named "%p%s%s%s%s%n"[5]). Modern languages, such asC++20 and later, tend to include format specifications as a part of the language syntax,[6] which restore type safety in formatting to an extent, and allow the compiler to detect some invalid combinations of format specifiers and data types at compile time.
In 1973,printf was included as a C standard library routine as part ofVersion 4 Unix.[7]
In 1990, theprintfshellcommand, modeled after the C standard library function, was included with4.3BSD-Reno.[8] In 1991, aprintf command was included with GNU shellutils (now part ofGNU Core Utilities).
In 2004,Java 5.0 (1.5) released, which extended the classjava.io.PrintStream, adding a method namedprintf() which functions analogously toprintf() in C. Thus to print a formatted string to the standard output stream, one usesSystem.out.printf(). Java further introduced the methodformat to its string classjava.lang.String.[9]
The need to do something about the range of problems resulting from lack of type safety has prompted attempts to make the C++ compilerprintf-aware.
The-Wformat option ofGNU Compiler Collection (GCC) allowscompile time checks toprintf calls, enabling the compiler to detect a subset of invalid calls (and issue either a warning or an error, terminating compiling, as set by other flags).[10]
Since the compiler is inspectingprintf format specifiers, enabling this effectively extends the C++ syntax by making formatting a part of it.
To address usability issues with the existingC++input/output support, and to avoid safety issues of printf[11] theC++ standard library was revised[12] to support a new type-safe formatting starting withC++20.[13] The approach ofstd::format() resulted from incorporating Victor Zverovich'slibfmt[14] API into the language specification[15] (Zverovich wrote[16] the first draft of the new format proposal); consequently,libfmt is an implementation of the C++20 format specification. InC++23, another function,std::print() andstd::println(), was introduced that combines formatting and outputting and therefore is a functional replacement forprintf().[17] However, no analogous::scanf() modernization has been introduced, though one has been proposed based onscnlib.[18]
As the format specification has become a part of the language syntax, a C++ compiler is able to prevent invalid combinations of types and format specifiers in many cases. Unlike the-Wformat option, this is not an optional feature.
The format specification oflibfmt andstd::format() is, in itself, an extensible "mini-language" (referred to as such in the specification),[19] an example of adomain-specific language. As such,std::print, completes a historical cycle; bringing the state-of-the-art (as of 2024) back to what it was in the case of Fortran's firstPRINT implementation in the 1950s.
Formatting of a value is specified as markup in the format string. For example, the following outputsYour age is and then the value of the variableage in decimal format.
printf("Your age is %d",age);
The syntax for a format specifier is:
%[parameter][flags][width][.precision][length]type
The parameter field is optional. If included, then matching specifiers to values isnot sequential. The numeric valuen selects the n-th value parameter. This is aPOSIX extension; notC99.[citation needed]
| Text | Description |
|---|---|
| n$ | n is the index of the value parameter toserialize using this format specifier |
This field allows for using the same value multiple times in a format string instead of having to pass the value multiple times. If a specifier includes this field, then subsequent specifiers must also.
For example,
printf("%2$d %2$#x; %1$d %1$#x",16,17);
outputs:17 0x11; 16 0x10
This field is very useful forlocalizing messages to differentnatural languages that use differentword orders.
InWindows API, support for this feature is via a different function,printf_p.
The flags field can be zero or more of (in any order):
| Text | Description |
|---|---|
| - | Left-align the output of this placeholder; default is to right-align the output |
| + | Prepends a plus sign for a positive value; by default a positive value does not have a prefix |
(space) | Prepends a space character for a positive value; ignored if the+ flag exists; by default a positive value does not have a prefix |
| 0 | When the 'width' option is specified, prepends zeros instead of spaces for numeric types; for example,printf("%4X",3) produces" 3", whileprintf("%04X",3); produces"0003" |
| ' | The integer or exponent of a decimal has the thousands grouping separator applied |
| # | Alternate form: Forg andG types, trailing zeros are not removed Forf,F,e,E,g,G types, the output always contains a decimal point Foro,x,X types, the text0,0x,0X, respectively, is prepended to non-zero numbers |
The width field specifies theminimum number of characters to output. If the value can be represented in fewer characters, then the value is left-padded with spaces so that output is the number of characters specified. If the value requires more characters, then the output is longer than the specified width. A value is never truncated.
For example,printf("%3d",12); specifies a width of 3 and outputs12 with a space on the left to output 3 characters. The callprintf("%3d",1234); outputs1234 which is 4 characters long since that is the minimum width for that value even though the width specified is 3.
If the width field is omitted, the output is the minimum number of characters for the value.
If the field is specified as*, then the width value is read from the list of values in the call.[20] For example,printf("%*d",3,10); outputs 10 (<space>10) where the second parameter,3, is the width (matches with*) and10 is the value toserialize (matches withd).
Though not part of the width field, a leading zero is interpreted as the zero-padding flag mentioned above, and a negative value is treated as the positive value in conjunction with the left-alignment- flag also mentioned above.
The width field can be used to format values as a table (tabulated output). But, columns do not align if any value is larger than fits in the width specified. For example, notice that the last line value (1234) does not fit in the first column of width 3 and therefore the column is not aligned.
1 1 12 12123 1231234 123
The precision field usually specifies amaximum limit of the output, as set by the formatting type. Forfloating-point numeric types, it specifies the number of digits to the right of the decimal point to which the output should be rounded; for%g and%G it specifies the total number ofsignificant figures (before and after the decimal, not including leading or trailing zeroes) to round to. For thestring type, it limits the number of characters that should be output, after which the string is truncated.
The precision field may be omitted, or a numeric integer value, or a dynamic value when passed as another argument when indicated by an asterisk (*). For example,printf("%.*s",3,"abcdef"); outputsabc.
The length field can be omitted or be any of:
| Text | Description |
|---|---|
| hh | For integer types, causesprintf to expect anint-sized integer argument which was promoted from achar. |
| h | For integer types, causesprintf to expect anint-sized integer argument which was promoted from ashort. |
| l | For integer types, causesprintf to expect along-sized integer argument. For floating-point types, this is ignored.float arguments are always promoted todouble when used in avarargs call.[21] |
| ll | For integer types, causesprintf to expect along long-sized integer argument. |
| L | For floating-point types, causesprintf to expect along double argument. |
| z | For integer types, causesprintf to expect asize_t-sized integer argument. |
| j | For integer types, causesprintf to expect aintmax_t-sized integer argument. |
| t | For integer types, causesprintf to expect aptrdiff_t-sized integer argument. |
Platform-specific length options came to exist prior to widespread use of the ISO C99 extensions, including:
| Text | Description | Commonly found platforms |
|---|---|---|
| I | For signed integer types, causesprintf to expectptrdiff_t-sized integer argument; for unsigned integer types, causesprintf to expectsize_t-sized integer argument | Win32/Win64 |
| I32 | For integer types, causesprintf to expect a32-bit (double word) integer argument | Win32/Win64 |
| I64 | For integer types, causesprintf to expect a64-bit (quad word) integer argument | Win32/Win64 |
| q | For integer types, causesprintf to expect a 64-bit (quad word) integer argument | BSD |
ISO C99 includes theinttypes.h header file that includes a number ofmacros forcross-platformprintf coding. For example:printf("%"PRId64,t); specifies decimal format for a64-bit signedinteger. Since the macros evaluate to astring literal, and the compilerconcatenates adjacent string literals, the expression"%"PRId64 compiles to a single string.
Macros include:
| Macro | Description |
|---|---|
| PRId32 | Typically equivalent toI32d (Win32/Win64) ord |
| PRId64 | Typically equivalent toI64d (Win32/Win64),lld (32-bit platforms) orld (64-bit platforms) |
| PRIi32 | Typically equivalent toI32i (Win32/Win64) ori |
| PRIi64 | Typically equivalent toI64i (Win32/Win64),lli (32-bit platforms) orli (64-bit platforms) |
| PRIu32 | Typically equivalent toI32u (Win32/Win64) oru |
| PRIu64 | Typically equivalent toI64u (Win32/Win64),llu (32-bit platforms) orlu (64-bit platforms) |
| PRIx32 | Typically equivalent toI32x (Win32/Win64) orx |
| PRIx64 | Typically equivalent toI64x (Win32/Win64),llx (32-bit platforms) orlx (64-bit platforms) |
The type field can be any of:
| Text | Description |
|---|---|
| % | Output a literal% character; does not accept flags, width, precision or length fields |
| d,i | (signed)int formatted as decimal;%d and%i are synonymous except when used withscanf |
| u | unsigned int formatted as decimal. |
| f,F | double formatted asfixed-point;f andF only differs in how the strings for an infinite number orNaN are printed (inf,infinity andnan forf;INF,INFINITY andNAN forF) |
| e,E | double formatted as in exponential notationd.ddde±dd;E results inE rather thane to introduce the exponent; the exponent always contains at least two digits; if the value is zero, the exponent is00; in Windows, the exponent contains three digits by default, e.g.1.5e002, but this can be altered by Microsoft-specific_set_output_format function |
| g,G | double formatted as either fixed-point or exponential notation, whichever is more appropriate for its magnitude;g uses lower-case letters,G uses upper-case letters; this type differs slightly from fixed-point notation in that insignificant zeroes to the right of the decimal point are not included, and that the precision field specifies the total number of significant digits rather than the digits after the decimal; the decimal point is not included on whole numbers |
| x,X | unsigned int formatted ashexadecimal;x uses lower-case letters andX uses upper-case |
| o | unsigned int formatted asoctal |
| s | null-terminated string |
| c | char |
| p | Pointer formatted in an implementation-defined way |
| a,A | double in hexadecimal notation, starting with0x or0X.a uses lower-case letters,A uses upper-case letters[22][23] |
| n | Outputs nothing but writes the number of characters written so far into an integerpointer parameter; inJava this prints anewline[24] |
A common way to handle formatting with a custom data type is to format the custom data type value into astring, then use the%s specifier to include the serialized value in a larger message.
Some printf-like functions allow extensions to theescape-character-basedmini-language, thus allowing the programmer to use a specific formatting function for non-builtin types. One is the (nowdeprecated)glibc'sregister_printf_function(). However, it is rarely used due to the fact that it conflicts withstatic format string checking. Another isVstr custom formatters, which allows adding multi-character format names.
Some applications (like theApache HTTP Server) include their own printf-like function, and embed extensions into it. However these all tend to have the same problems thatregister_printf_function() has.
TheLinux kernelprintk function supports a number of ways to display kernel structures using the generic%p specification, byappending additional format characters.[25] For example,%pI4 prints anIPv4 address in dotted-decimal form. This allows static format string checking (of the%p portion) at the expense of full compatibility with normal printf.
Extra value arguments are ignored, but if the format string has more format specifiers than value arguments passed, the behavior is undefined. For some C compilers, an extra format specifier results in consuming a value even though there isn't one which allows theformat string attack. Generally, for C, arguments arepassed on the stack. If too few arguments are passed, then printf can read past the end of the stack frame, thus allowing an attacker to read the stack.
Some compilers, likethe GNU Compiler Collection, willstatically check the format strings of printf-like functions and warn about problems (when using the flags-Wall or-Wformat). GCC will also warn about user-defined printf-style functions if the non-standard "format"__attribute__ is applied to the function.
The format string is often astring literal, which allowsstatic analysis of the function call. However, the format string can be the value of avariable, which allows for dynamic formatting but also a security vulnerability known as anuncontrolled format string exploit.
Although an output function on the surface,printf allows writing to a memory location specified by an argument via%n. This functioning is occasionally used as a part of more elaborate format-string attacks.[26]
The%n functioning also makesprintf accidentallyTuring-complete even with a well-formed set of arguments. A game of tic-tac-toe written in the format string is a winner of the 27thIOCCC.[27]
Variants ofprintf in the C standard library include:fprintf outputs to afile instead of standard output.
sprintf writes to astring buffer instead of standard output.
snprintf provides a level of safety oversprintf since the caller provides a lengthn that is the length of the output buffer in bytes (including space for the trailing null character).
asprintf provides for safety by accepting a stringhandle (char**) argument. The functionallocates a buffer of sufficient size to contain the formatted text and outputs the buffer via the handle.
For each function of the family, including printf, there is also a variant that accepts a singleva_list argument rather than a variable list of arguments. Typically, these variants start with "v". For example:vprintf,vfprintf,vsprintf.
Generally, printf-like functions return the number of bytes output or -1 to indicate failure.[28]
The following list includes notable programming languages that provide (directly or via a standard library) functioning that is the same or similar to the C printf-like functions. Excluded are languages that use format strings that deviate from the style in this article (such asAMPL andElixir), languages that inherit their implementation from theJava virtual machine (JVM) or other environment (such asClojure andScala), and languages that do not have a standard native printf implementation but have external libraries which emulate printf behavior (such asJavaScript).
string.format)sprintf object)printf,sprintf, andfmt)% operator)[31]format command)xp_sprintf)print() andFileStream.printf())printf-style String Formatting",The Python Standard Library, Python Software Foundation, retrieved24 February 2021std::fprintfprintf: print formatted output – System Interfaces Reference,The Single UNIX Specification, Version 5 fromThe Open GroupFormatter specification inJava 1.5printf(1) builtin