1950s: Fortran Early programming languages like
Fortran used special statements with different syntax from other calculations to build formatting descriptions. In this example, the format is specified on line , and the command refers to it by line number: PRINT 601, IA, IB, AREA 601 FORMAT (4H A= ,I5,5H B= ,I5,8H AREA= ,F10.2, 13H SQUARE UNITS) Hereby: • indicates a
string of 4
characters " A= " ( means
Hollerith Field); • indicates an
integer field of width 5; • indicates a
floating-point field of width 10 with 2 digits after the decimal point. An output with input arguments , , and might look like this: A= 100 B= 200 AREA= 1500.25 SQUARE UNITS
1960s: BCPL and ALGOL 68 In 1967,
BCPL appeared. Its library included the routine which looked like any other function call. An example application looks like this: WRITEF("%I2-QUEENS PROBLEM HAS %I5 SOLUTIONS*N", NUMQUEENS, COUNT) Hereby: • indicates an
integer of width 2 (the order of the format specification's field width and type is reversed compared to C's ); • indicates an integer of width 5; • is a BCPL
language escape sequence representing a
newline character (for which C uses the escape sequence ).
ALGOL 68 also had a function api, but used special syntax for the format: printf(($"Color "g", number1 "6d,", number2 "4zd,", hex "16r2d,", float "-d.2d,", unsigned value"-3d"."l$, "red", 123456, 89, BIN 255, 3.14, 250)); Using normal function syntax for the printing simplifies the language, and allows the printing to be implemented in the language itself. In most newer languages of that era I/O is not part of the syntax. However the format was usually not checked to see if it matched the type (or even the number) of values being printed and mismatches resulted in crashes, security exploits, and 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").
1970s: C In 1973, was included as a C standard library routine as part of
Version 4 Unix.
1990s: Shell command In 1990, the
printf shell command, modeled after the C standard library function, was included with
4.3BSD-Reno. In 1991, a command was included with GNU shellutils (now part of
GNU Core Utilities) and the syntax (options, arguments, etc.) of this "
shell command" are different from the
C-Language function e.g.: the
"format section" does not use the
positional arguments with a "$" symbol (n$) in the same way as printf() function: str="AA BB CC" # simple string with 3 fields set -- $str # convert fields to positional parameters printf "%s " $2 $3 $1; echo # in C printf() uses 2$ 3$.. • prints: BB CC AA
2000s: Java In 2004,
Java 5.0 (1.5) released, which extended the class java.io.PrintStream, adding a method named printf() which functions analogously to printf() in C. Thus to print a formatted string to the standard output stream, one uses System.out.printf(). Java further introduced the method format to its string class java.lang.String.
2000s: -Wformat safety The need to do something about the range of problems resulting from lack of type safety has prompted attempts to make compilers -aware. The option of
GNU Compiler Collection (GCC) allows
compile time checks to calls, enabling the compiler to detect a subset of invalid calls (and issue either a warning or an error, terminating compilation, as set by other flags). Because the compiler inspects format specifiers, this feature effectively extends
static analysis in C to include formatting aspects.
2020s: std::print C++ added
input/output support using the operator to avoid safety issues of printf. This used the type of the arguments to choose which code to execute to print them, avoiding the crashes that are possible with format strings. However, the syntax can be verbose (especially for setting options like precision), and remains available in C++ and is often used instead.
C++20 added a new API which uses a string consisting of verbatim text and placeholders followed by the values to print. The format string uses curly braces instead of percent signs, a syntax popularized by
Python: :{{code|lang=C++|std::format("The hex value of {} is {:x}.", name, value)}} The recommended implementation is from Victor Zverovich's
fmtlib which at compile time converts the string and argument types into an optimized formatting object, this is type-safe and syntax errors are detected at compile time.
C++23 introduced the functions and which combined formatting and outputting, and is therefore a functional replacement for . It is possible to make a translator from a printf %-string to the same formatting object and this could produce a type-safe , but this was also not added to the spec. No analogous modernization has been introduced, though one has been proposed based on
scnlib. These functions (plus ) can print floating point accurately using the least number of trailing digits possible, an ability long missing from . Another useful feature is that they ignore the
locale. == Format specifier ==