8

This question already has an answer here:

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.

marked as duplicate by Steven Burnap, David Hammen, gnat, Adam Zuckerman, user22815 May 4 '16 at 6:13

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 19
    Your premise that printf is C's most basic output function is false. In addition to puts, there's also putchar. – 8bittree May 3 '16 at 18:05
  • 14
    The "f" at the end stands for "formatted," as in "print, formatted." The extra extra letter is made up for by the fact that the creat() function has no "e" on the end. :-) – Blrfl May 3 '16 at 18:52
  • 2
    Voting to close as primarily opinion-based. – David Hammen May 3 '16 at 21:39
  • 1
    @DavidHammen this is clearly not POB, there are historical reasons for it. Also, the dupe target explains why. – user22815 May 4 '16 at 6:13
  • 3
    I have voted to reopen as (a) the question marked as duplicate is focussed on a different aspect of the naming decision and (b) while the answers there touch briefly on the question here they don't go into sufficient depth. – Jules May 4 '16 at 10:20
19

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 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.

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.

19

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

6

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.