3
#include <stdio.h>
/* replace tabs and backspaces with visible characters */

main()

{
    int c;

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

Why was I not able to see \b backspace signature when I press the backspace?

chris
  • 60,560
  • 13
  • 143
  • 205
Joseph Lee
  • 529
  • 1
  • 5
  • 10
  • a couple of comments on your code: `getchar` is evil, conditions with side-effets are evil, and if cascades are evil as well (if they can be conveniently replaced by switch/case statements, which is the `case` here. :D (pun intended) – Andreas Grapentin Jan 08 '13 at 10:37
  • `Ctrl` + `Backspace` = `^H` – serghei Nov 21 '14 at 00:16

3 Answers3

1

You need to learn about else, that if-ladder is pretty scary.

And your terminal probably doesn't send a single backspace character, it can be a bit complicated how actual terminal programs represent that kind of "special" key (delete is another favorite).

unwind
  • 391,730
  • 64
  • 469
  • 606
1

If you're on a unix-like system, you probably want to read this: http://en.wikipedia.org/wiki/Cooked_mode

On other operating systems, I don't know, but they are also likely to do things to your input.

Art
  • 19,807
  • 1
  • 34
  • 60
1

Some of the characters handled by Terminal. So you can't get control over it. Check this answer.

I tried in my MAC terminal. But i didn't get the value 127 or 8 like, in this answer. I got 32 for backspace character. So when i tried the if condition with 32, it printed out the \b value.

if (c == 32 || c == 8)
    printf("\\b");
Community
  • 1
  • 1
arthankamal
  • 6,341
  • 4
  • 36
  • 51