Printf supremacy

Not all functions are equal. Some simple, others not, some are safe, not all, some have side effects, and some don't. There is, however, a class of functions that are above all others, a higher cast, respected by compilers and language designers. Think of the most priviledged functions in Pascal, those that are blessed with the ability to receive variable number of arguments. Or in case of C, of these only functions to get their arguments dependently-typed-checked. Read along as I will try to convince you, the printf supremacy is real.

Printf functions are the ones used to output formatted text on the terminal. I claim that they are priviledged in many languages and their special treatment is out of place.

It started with Fortran. There was no single function to do formatted output in Fortran, there was a special statement to do that. Here is how it looked like:

      WRITE OUTPUT TAPE 6, 601, IA, IB, IC, AREA
      FORMAT (4H A= ,I5,5H  B= ,I5,5H  C= ,I5,
      &       8H  AREA= ,F10.2, 13H SQUARE UNITS)
  
Needless to say, it was a distinguished language element, both the programmer and the compiler must have had a respect for it. And the statement itself looked quite involved, for a good reason -- check out that 601 after the WRITE keyword, it stands for the line number of the relevant FORMAT form!

Going forward in history... Printf is so complicated it has been considered an attack surface. This seems to be the primary reason for it being so extraordinary, for it is hard to express it in native terms. But when we do make it special-case, we lie to programmers. I personally think that printf is a hack, it's ugly, and does not belong in modern languages, but it is here anyways, and we should aknowledge that it is a little special.

References

  1. printf's history
  2. First language to have printf
  3. Mechanics of Algol's printf
  4. Confused user tries to understand C's printf
  5. Critique of printf (rust case)
  6. Attack on printf


return home