K&R Solutions - 1.10

Q: Write a program to copy its input to its output, replacing each tab by \t , each backspace by \b , and each backslash by \\ . This makes tabs and backspaces visible in an unambiguous way.

A:

#include <stdio.h>

int main(void)
{
  char c;

  while ((c = getchar()) != EOF) {
    if (c == '\t') {
      printf("\\t");
    } else if (c == '\b') {
      printf("\\b");
    } else if (c == '\\') {
      printf("\\\\");
    } else {
      putchar(c);
    }
  }

  return 0;
}
tab	example
tab\texample
\ example
\\ example
Backspace example doesn't work!
Backspace example doesn't work!

Denis Richie standing by the computer where he invented C

Notes:

Your terminal emulator takes care of automatically backspacing for you, so the final part of this exercise is long-obsolete.


Explicit backspaces were a problem Richie dealt with when they were developing C and Unix in the 70s. Their office computer didn't have a monitor, only a printer (teletype) which didn't show the characters you had already backspaced (in fact, it added the whitespace \b which was applied as a character deletion only after you saved the line); you just had to try to keep track of your corrections in your head.


According to people around back then, the reason C tends to be so terse, and keywords/variable names tended to be so short, is that backspacing was so frustratingly difficult to keep track of that they wanted to keep the lines as short as possible. This is contrary to the modern style, where clear names are considered extremely important.


It's pretty easy (and fun) to run Unix 6 on an emulator, to get a bit of a taste of what these people were dealing with back then. After trying to write a few small programs, you'll get a pretty good feeling for why old Unix culture discouraged long names.