What happens when \t meets \b in C?

147 Views Asked by At

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.

1

There are 1 best solutions below

1
Keith Thompson On

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.)