Syntax rewrite, more docs
[tcpl.git] / ch1 / 10.c
blobdcfcddbe4baa563eb3439ecad0eb50435d5155ec
1 /*
2 * Example 1-10:
3 * Write a program to copy its input to its output, replacing each tab by \t, each backspace by \b,
4 * and each backslash by \\. This makes tabs and backspaces visible in an unambiguous way.
5 */
7 // NOTE: On most modern systems, backspaces will not show up, because of buffered output.
9 #include <stdlib.h>
10 #include <stdio.h>
13 * Print this when we need to show a special character.
14 * It itself is escaped, because a backslash is also a special character!
16 #define ESC_CHAR '\\'
18 int
19 main(void)
21 // Read all input until EOF.
22 int c;
23 while ((c = getchar()) != EOF)
24 // Show special characters by printing ESC_CHAR and then a letter.
25 switch (c) {
26 case '\t':
27 putchar(ESC_CHAR);
28 putchar('t');
29 break;
30 case '\b':
31 putchar(ESC_CHAR);
32 putchar('b');
33 break;
34 case ESC_CHAR:
35 putchar(ESC_CHAR);
36 putchar(ESC_CHAR);
37 break;
38 default:
39 putchar(c);
42 return EXIT_SUCCESS;