AFAIK, C does not have a print
function. The most basic we can get is printf
. So, is there a reason why this function is not simply named print
, instead of printf
?
Note that this question is about scanf
/readf
and printf
/writef
. This one is about printf
/print
.
3 Answers 3
Let's go back to the K&R roots:
Origin
In the tutorial chapter, on page 11 of original K&R, you'll find a hint on the origin of the function:
By the way,
printf
is not part of the C language; there is no input or output defined in C itself. There is nothing magic aboutprintf
; it's just a useful function that is part of the standard library of routines that are normally accessible to C programs.
The wording "accessible" suggests that it could be based on libraries shared with other programming languages as suggested by Jules, or on shared OS libraries. As B and C both are closely linked to the development of UNIX, and the same people, D.Ritchie, B.W.Kernighan and K.Thompson closely worked together on these matters, it is not surprising that they use similar naming conventions.
Why the f of printf ?
The title of section 7.3 Formatted output - printf on page 145 of original K&R strongly suggest that the f
stands for formatted:
Searching in other sources will show that B's and C's printf
both seem to originate from BCPL's writef
function which used already in 1966 the %
formatting character.
Also worth to note that Algol68 also adopted printf
function for formatted output. Yet the formatting logic was a little different.
The function "printf" was inherited by C from the B standard library. In B it wasn't the only such function, for example there was also a "printn" for printing numbers. See a reference for the language from an early unix version here: https://www.bell-labs.com/usr/dmr/www/kbman.html
It's not the most basic printing function. The most basic printing functions would be puts
and putchar
which print a string and char respectively.
f is for formatted. printf
(unlike puts
or putchar
) prints formatted output, hence printf. For example it can print an int in hexadecimal, or a float rounded to three decimal places, or a string left padded. (Yep, you read that right: in the 1970s C had a single string function in the standard library doing more than an entire npm module. ;)
C didn't do printf
first. B has a function of the same name with similar functionality. Wikipedia identifies an even earlier inspiration in BCPL's writef
.
Explore related questions
See similar questions with these tags.
printf
is C's most basic output function is false. In addition toputs
, there's alsoputchar
.creat()
function has no "e" on the end. :-)