Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Printf
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==History== ===1950s: Fortran=== Early programming languages like [[Fortran]] used special statements with different syntax from other calculations to build formatting descriptions.<ref name="Sayre_1956">{{cite book |title=The FORTRAN Automatic Coding System for the IBM 704 EDPM: Programmer's Reference Manual |publisher=Applied Science Division and Programming Research Department, [[International Business Machines Corporation]] |location=New York, USA |date=October 15, 1956 |editor-first=David |editor-last=Sayre |editor-link=David Sayre |author-first1=John Warner |author-last1=Backus |author-link1=John Warner Backus |author-first2=R. J. |author-last2=Beeber |author-first3=Sheldon F. |author-last3=Best |author-first4=Richard |author-last4=Goldberg |author-first5=Harlan L. |author-last5=Herrick |author-first6=R. A. |author-last6=Hughes |author-first7=L. B. |author-last7=Mitchell |author-first8=Robert A. |author-last8=Nelson |author-first9=Roy |author-last9=Nutt |author-link9=Roy Nutt |author-first10=David |author-last10=Sayre |author-link10=David Sayre |author-first11=Peter B. |author-last11=Sheridan |author-first12=Harold |author-last12=Stern |author-first13=Irving |author-last13=Ziller |pages=26β30 |url=http://archive.computerhistory.org/resources/text/Fortran/102649787.05.01.acc.pdf |access-date=July 4, 2022 |url-status=live |archive-url=https://web.archive.org/web/20220704193549/http://archive.computerhistory.org/resources/text/Fortran/102649787.05.01.acc.pdf |archive-date=July 4, 2022}} (2+51+1 pages)</ref> In this example, the format is specified on line {{samp|601}}, and the {{code|PRINT|fortran}}{{efn|According to the 1956 Fortran manual{{r|Sayre_1956}}, the {{code|PRINT|fortran}} command prints on the attached [[line printer|printer]]. The manual also introduces the command {{code|WRITE OUTPUT TAPE|fortran}} that also uses the {{code|FORMAT|fortran}} statement to write on a [[Magnetic-tape data storage|tape unit]].}} command refers to it by line number: <syntaxhighlight lang="fortranfixed"> PRINT 601, IA, IB, AREA 601 FORMAT (4H A= ,I5,5H B= ,I5,8H AREA= ,F10.2, 13H SQUARE UNITS) </syntaxhighlight> Hereby: * {{code|4H|fortran}} indicates a [[String (computer science)|string]] of 4 [[character (computer science)|characters]] <code>" A= "</code> ({{code|H}} means [[Hollerith constant|Hollerith Field]]); * {{code|I5|fortran}} indicates an [[Integer (computer science)|integer]] field of width 5; * {{code|F10.2|fortran}} indicates a [[floating-point]] field of width 10 with 2 digits after the decimal point. An output with input arguments {{code|100}}, {{code|200}}, and {{code|1500.25}} might look like this: <syntaxhighlight lang="output"> A= 100 B= 200 AREA= 1500.25 SQUARE UNITS </syntaxhighlight> ===1960s: BCPL and ALGOL 68=== <!-- Question: did the BCPL writef library routine already appear in 1967... or only later? --> In 1967, [[BCPL]] appeared.<ref>{{cite web|url=http://www.cl.cam.ac.uk/users/mr/BCPL.html|title=BCPL|website=cl.cam.ac.uk|access-date=19 March 2018}}</ref> Its library included the {{code|writef}} routine.<ref>{{cite book |last1=Richards |first1=Martin |last2=Whitby-Strevens |first2=Colin |title=BCPL - the language and its compiler |date=1979 |publisher=Cambridge University Press |page=[https://archive.org/details/richards1979bcpl/page/n57 50] |url=https://archive.org/details/richards1979bcpl}}</ref> An example application looks like this: <syntaxhighlight lang="text"> WRITEF("%I2-QUEENS PROBLEM HAS %I5 SOLUTIONS*N", NUMQUEENS, COUNT) </syntaxhighlight> Hereby: * {{code|%I2}} indicates an [[Integer (computer science)|integer]] of width 2 (the order of the format specification's field width and type is reversed compared to C's {{code|printf}}); * {{code|%I5}} indicates an integer of width 5; * {{code|*N}} is a BCPL ''language'' [[escape sequence]] representing a [[newline]] character (for which C uses the escape sequence {{code|\n}}). In 1968, [[ALGOL 68]] had a more function-like [[API]], but still used special syntax (the {{code|$}} delimiters surround special formatting syntax): <syntaxhighlight lang="cpp"> printf(($"Color "g", number1 "6d,", number2 "4zd,", hex "16r2d,", float "-d.2d,", unsigned value"-3d"."l$, "red", 123456, 89, BIN 255, 3.14, 250)); </syntaxhighlight> In contrast to Fortran, using normal function calls and data types simplifies the language and compiler, and allows the implementation of the input/output to be written in the same language. These advantages were thought to outweigh the disadvantages (such as a complete lack of [[type 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<ref>{{cite web|url=https://owasp.org/www-community/attacks/Format_string_attack |title = Format String Attack}}</ref> 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"<ref>{{cite web|url=https://www.bleepingcomputer.com/news/security/iphone-bug-breaks-wifi-when-you-join-hotspot-with-unusual-name/|title = iPhone Bug Breaks WiFi When You Join Hotspot With Unusual Name}}</ref>). Modern languages, such as [[C++20]] and later, tend to include format specifications as a part of the language syntax,<ref>{{cite web|url=https://en.cppreference.com/w/cpp/utility/format/spec|title=C++20 Standard format specification}}</ref> 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. ===1970s: C=== In 1973, {{code|printf}} was included as a C standard library routine as part of [[Version 4 Unix]].<ref name="reader">{{cite tech report |first1=M. D. |last1=McIlroy |author-link1=Doug McIlroy |year=1987 |url=http://www.cs.dartmouth.edu/~doug/reader.pdf |title=A Research Unix reader: annotated excerpts from the Programmer's Manual, 1971β1986 |series=CSTR |number=139 |institution=Bell Labs}}</ref> ===1990s: Shell command=== In 1990, the <code>[[printf (Unix)|printf]]</code> [[shell (computing)|shell]] [[command (computing)|command]], modeled after the C standard library function, was included with [[4.3BSD-Reno]].<ref>{{cite web |title=printf (4.3+Reno BSD) |url=https://man.freebsd.org/cgi/man.cgi?query=printf&apropos=0&sektion=0&manpath=4.3BSD+Reno&arch=default&format=html |website=man.freebsd.org |access-date=2024-04-01}}</ref> In 1991, a {{code|printf}} command was included with GNU shellutils (now part of [[GNU Core Utilities]]). ===2000s: -Wformat safety=== The need to do something about the range of problems resulting from lack of type safety has prompted attempts to make the C++ compiler {{code|printf}}-aware. The {{kbd|-Wformat}} option of [[GNU Compiler Collection|GCC]] allows compile-time checks to {{code|printf}} calls, enabling the compiler to detect a subset of invalid calls (and issue either a warning or an error, stopping the compilation altogether, depending on other flags).<ref>{{cite manual| section-url= https://gcc.gnu.org/onlinedocs/gcc-14.2.0/gcc/Warning-Options.html#index-Wformat | url= https://gcc.gnu.org/onlinedocs/gcc-14.2.0/gcc/ | section= 3.8 Options to Request or Suppress Warnings | title= GCC 14.2 Manual | author=Free Software Foundation | author-link=Free Software Foundation | publisher= self-published | year= 2024 | accessdate= 2025-02-12 }}</ref> Since the compiler is inspecting {{code|printf}} format specifiers, enabling this effectively extends the C++ syntax by making formatting a part of it. ===2020s: std::print === To address usability issues with the existing [[C++]] [[input/output (C++)|input/output support]], as well as avoid safety issues of printf<ref>{{cite web|url=https://hownot2code.wordpress.com/2016/08/10/beware-of-printf|title=How Not to Code: Beware of printf|date=10 August 2016 }}</ref> the [[C++ standard library]] was revised<ref>{{cite web|url=https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2216r2.html|title=C++20 Format improvements proposal to enable compile-time checks}}</ref> to support a new type-safe formatting starting with [[C++20]].<ref>{{cite web|url=https://en.cppreference.com/w/cpp/utility/format/format|title=C++20 std::format}}</ref> The approach of {{code|std::format|cpp}} resulted from incorporating Victor Zverovich's {{code|libfmt}}<ref>{{cite web|url=https://fmt.dev|title=libfmt: a modern formatting library}}</ref> API into the language specification<ref>{{cite web|url=https://www.accu.org/journals/overload/29/166/collyer/|title=C++20 Text Formatting: An Introduction}}</ref> (Zverovich wrote<ref>{{cite web|url=https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0645r10.html|title=C++ Format Proposal History}}</ref> the first draft of the new format proposal); consequently, {{code|libfmt}} is an implementation of the C++20 format specification. In [[C++23]], another function, {{code|std::print|cpp}}, was introduced that combines formatting and outputting and therefore is a functional replacement for {{code|printf()}}.<ref>{{cite web|url=https://en.cppreference.com/w/cpp/io/print|title=C++ print}}</ref> 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 {{kbd|-Wformat}} option, this is not an optional feature. The format specification of {{code|libfmt}} and {{code|std::format|cpp}} is, in itself, an extensible "mini-language" (referred to as such in the specification),<ref>{{cite web|url=https://fmt.dev/6.2.1/syntax.html#format-specification-mini-language|title=Format Specification Mini-Language}}</ref> an example of a [[domain-specific language]]. As such, {{code|std::print|cpp}}, completes a historical cycle; bringing the state-of-the-art (as of 2024) back to what it was in the case of Fortran's first {{code|PRINT|fortran}} implementation in the 1950s<!--overly colorful and debatable-->.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)