Escape Codes

To explain the reason for a number of the escape codes in this tutorial, I’m going to describe a bit more of the history of the C Language. At the time C was being developed, the computer industry was in the transition from using punch cards for input to hardcopy terminals. A hardcopy terminal was essentially a printer with a keyboard. It can with an attached stand and they were about desk height. You could sit at one in your office chair and type away. The last time I used a hardcopy terminal was 1994.

The way a hardcopy terminal worked was that when you hit a key on the keyboard, it printed the character on the paper. So, you would use it to create and edit files as well as print the results of programs. The problem this method of input posed is that once the character was printed, it could not be erased the way it is on your screen. So, if you typed Hello World followed by 5 backspaces this is what you would see on the terminal:

Hello World\dlroW

The backslash indicated that it was removing the characters after it from the input. That’s why the go in reverse order, because the first backspace removed the d, the second the l, and so on. If you then typed There you would see:

Hello World\dlroW/There

The forward slash told you that it had resumed adding to the input. They did take a bit of getting used to, but weren’t that bad to work with.

Here is today’s program (I called it escape.c):

int main()

printf(“1\nHello\f World!\n”);
printf(“2\nHello\r World!\n”);
printf(“3\nHello\r\f World!\n”);
printf(“4\nHello\t World!\n”);
printf(“5\nHello\v World!\n”);
printf(“6\nHello\\n World!\n”);
printf(“7\n\”Hello World!\”\n”);
printf(“8\nHello \bWorld!\n”);
return 0;


The output looks like this:

Hello World!
Hello\n World!
“Hello World!”

In the last tutorial, we used the \n. This told the hardcopy terminal to feed the paper a line and move the printhead to the beginning of the line. All the escape codes used by printf() begin with \. printf() looks for it in the string literal as an indicator that the next character is going to be an escape code. The remaining escape codes I used are as follows:

\f told the hardcopy terminal to feed the paper a line.
\r told the hardcopy terminal to move to the beginning of the line. So, a \r\f is the same as a \n.
\t told the hardcopy terminal to go to the next tabstop. I believe most terminals had 8 characters between tabstops. This is useful if you want to line up information vertically on the page.
\v told the hardcopy terminal to do a vertical tab. The terminals I used would move down to the next line which was one more that a multiple of 4 (1,5,9,13,17…).
\\ told the hardcopy terminal to put 1 backslash in the output. This is necessary because the first \ is always taken to mean an escape code is next.
\” told the hardcopy terminal to put a ” in the output. This is necessary because the ” is used to identify the beginning and end of a string literal. The \ tells the compiler that it is an escape code rather than a delimiter.

The reason these codes are still in use is that when terminals with CRT displays (monitors) were developed, the designers incorporated the same set of control codes that were used with hardcopy terminals. It was probably one of the biggest selling features of the CRT terminals. For the same reason, the codes have been incorporated into nearly all text based computer interfaces.

Try playing with the escape sequences and see if you can draw a shape.

Author: Ron

Homeschooling dad of 4 (ages 27 - 14), grampy to 3, WordPress core contributor, former farmboy & software developer by profession.