-1

I know what \t and \b mean.
But what will happen when a \b after a \t, and what will happen when multiple \b and \t combine.
Look at this code, It contains a variety of combinations of \t and \b:

printf("1\t1\t1\t1\n");
printf("---------------------------\n");
printf("1\t2\b\b3\n");
printf("\t\b1\n");

I got:

1       1       1       1
---------------------------
1      32
1

The two \b are displayed differently.
The results are confusing, with my existing knowledge simply can not explain.
I do not know how to understand \t and \b in the end.

wenmin92
  • 106
  • 1
  • 4
  • you need to start first by knowing exactly what your _correct_ output should look like – artm Mar 12 '17 at 00:49
  • @artm Thanks for your reply. But I do not know what is _right_ now. The output confused me. – wenmin92 Mar 12 '17 at 01:05
  • You also need to describe what device `stdout` actually is. For example, is it redirected to a file and, if so, how is the output viewed? Assuming it is not redirected, what display device is being used? Different devices (and device drivers) handle tabs differently and even backspaces (e.g. if the first character on the line) can be handled differently. And there are settings that affect tabbing. – Peter Mar 12 '17 at 02:05
  • @Peter Thanks. In the same environment, I can not explain the output. Here I use Windows Clang compiler, and the CLion console display the results. – wenmin92 Mar 12 '17 at 02:23
  • This really has nothing to do with C. You would get the same results if you simply copied the same data directly to the terminal. – rici Mar 12 '17 at 03:10
  • @rici In my windows CMD, I copy the "1\t2\b\b3", and I got "13", and the compiled program got "1 3", "" means backspace. – wenmin92 Mar 12 '17 at 03:39
  • When i said copy, i meant with the copy command from data in a file. Pasting the clipboard as *input* is not the same. – rici Mar 12 '17 at 04:07
  • @rici Sorry, I still still do not know how to operate. How to use the character `\b` in a file? – wenmin92 Mar 12 '17 at 04:21
  • If you have a unixlike environment, you can use the `printf` utility: `printf 'Hello, \bworld\n' > file.txt`. With native windows utilities, I don't know but it's hex `08` if you have a hex editor. (tab is hex `09`). – rici Mar 12 '17 at 06:42
  • @rici Yes, in the unixlike environment, it's OK. – wenmin92 Mar 12 '17 at 07:08

1 Answers1

2

printf("\b") sends a backspace character to standard output. printf("\t") sends a tab character to standard output. What happens from there it up to the environment; your program has no real control over it.

On a typical interactive output device, a backspace will move the cursor one column to the left (without erasing anything), and a tab character will move the cursor to the next tabstop (also without erasing anything). That should explain what you're seeing.

(Actually the output I get differs from what you've shown us, which makes me think either that there's something different about the terminal you're using, or you've transcribed the output incorrectly.)

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • I know, different terminal shows differently. But in one terminal, the Behavior is inconsistent. This is where I am confused. – wenmin92 Mar 12 '17 at 03:05