Computer History

{"content":{"id":137,"title":"K\u0026R Solutions - 1.10","body":"\u003cp\u003e\u003cb\u003eQ:\u003c/b\u003e Write a program to copy its input to its output, replacing each tab by \\t , each\r\nbackspace by \\b , and each backslash by \\\\ . This makes tabs and backspaces visible in an\r\nunambiguous way.\u003c/p\u003e\r\n\u003cp\u003e\u003cb\u003eA:\u003c/b\u003e\u003c/p\u003e \r\n\u003cpre\u003e\u003ccode class=\"cpp\"\u003e#include \u0026ltstdio.h\u0026gt\r\n\r\nint main(void)\r\n{\r\n char c;\r\n\r\n while ((c = getchar()) != EOF) {\r\n if (c == '\\t') {\r\n printf(\"\\\\t\");\r\n } else if (c == '\\b') {\r\n printf(\"\\\\b\");\r\n } else if (c == '\\\\') {\r\n printf(\"\\\\\\\\\");\r\n } else {\r\n putchar(c);\r\n }\r\n }\r\n\r\n return 0;\r\n}\r\n\u003c/code\u003e\u003c/pre\u003e\r\n\u003cpre\u003e\u003ccode class=\"cpp\"\u003etab\texample\r\ntab\\texample\r\n\\ example\r\n\\\\ example\r\nBackspace example doesn't work!\r\nBackspace example doesn't work!\r\n\u003c/code\u003e\u003c/pre\u003e\r\n\u003cbr\u003e\r\n\u003cimg src=\"/uploads/56.jpg\" class=\"card-image\"\u003e\r\n\u003cdiv class=\"card-image-caption\" style=\"color: black\"\u003eDenis Richie standing by the computer where he invented C\u003c/div\u003e\r\n\u003cp\u003e\u003cb\u003eNotes:\u003c/b\u003e\u003c/p\u003e\r\n\u003cp\u003eYour terminal emulator takes care of automatically backspacing for you, so the final part of this exercise is long-obsolete.\u003c/p\u003e\r\n\u003cbr\u003e\r\n\u003cp\u003eExplicit 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.\u003c/p\u003e\r\n\u003cbr\u003e\r\n\u003cp\u003eAccording 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.\u003c/p\u003e\r\n\u003cbr\u003e\r\n\u003cp\u003eIt'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.\u003c/p\u003e","publication_date":"2018-06-30T15:00:00.000Z","created_at":"2018-06-23T03:55:54.000Z","updated_at":"2018-10-04T23:34:10.000Z","user_id":1,"rating":null},"tags":"\u003ca class=\"changeable-title\" href=\"/q?tag=k%26r\"\u003ek\u0026amp;r\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=programming\"\u003eprogramming\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=answers\"\u003eanswers\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=c\"\u003ec\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=unix\"\u003eunix\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=computer_history\"\u003ecomputer_history\u003c/a\u003e"}

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.