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!
===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.
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)