AFAIK, C does not have a
printf. So, is there a reason why this function is not simply named
Note that this question is about
writef. This one is about
Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle. It only takes a minute to sign up.Sign up to join this community
Let's go back to the K&R roots:
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,
printfis not part of the C language; there is no input or output defined in C itself. There is nothing magic about
printf; 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.
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
putchar which print a string and char respectively.
f is for formatted.
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