The "backspace" escape character '\b': unexpected behavior?

CLanguage AgnosticPrintfSpecial CharactersBackspace

C Problem Overview


So I'm finally reading through K&R, and I learned something within the first few pages, that there is a backspace escape character, \b.

So I go to test it out, and there is some very odd behavior:

#include <stdio.h>

main ()
{
    printf("hello worl\b\bd\n");
}

The output is

hello wodl

Can anyone explain this?

C Solutions


Solution 1 - C

Your result will vary depending on what kind of terminal or console program you're on, but yes, on most \b is a nondestructive backspace. It moves the cursor backward, but doesn't erase what's there.

So for the hello worl part, the code outputs

hello worl
^

...(where ^ shows where the cursor is) Then it outputs two \b characters which moves the cursor backward two places without erasing (on your terminal):

hello worl
^

Note the cursor is now on the r. Then it outputs d, which overwrites the r and gives us:

hello wodl
^

Finally, it outputs \n, which is a non-destructive newline (again, on most terminals, including apparently yours), so the l is left unchanged and the cursor is moved to the beginning of the next line.

Solution 2 - C

..........
^ <= pointer to "print head"

            /* part1 */
            printf("hello worl");

hello worl
^ <= pointer to "print head"

            /* part2 */
            printf("\b");

hello worl
^ <= pointer to "print head"

            /* part3 */
            printf("\b");

hello worl
^ <= pointer to "print head"

            /* part4 */
            printf("d\n");

hello wodl

^ <= pointer to "print head" on the next line

Solution 3 - C

If you want a destructive backspace, you'll need something like

"\b \b"

i.e. a backspace, a space, and another backspace.

Solution 4 - C

Not too hard to explain... This is like typing hello worl, hitting the left-arrow key twice, typing d, and hitting the down-arrow key.

At least, that is how I infer your terminal is interpeting the \b and \n codes.

Redirect the output to a file and I bet you get something else entirely. Although you may have to look at the file's bytes to see the difference.

[edit]

To elaborate a bit, this printf emits a sequence of bytes: hello worl^H^Hd^J, where ^H is ASCII character #8 and ^J is ASCII character #10. What you see on your screen depends on how your terminal interprets those control codes.

Solution 5 - C

Use a single backspace after each character printf("hello wor\bl\bd\n");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOregonTrailView Question on Stackoverflow
Solution 1 - CT.J. CrowderView Answer on Stackoverflow
Solution 2 - CpmgView Answer on Stackoverflow
Solution 3 - CPeter K.View Answer on Stackoverflow
Solution 4 - CNemoView Answer on Stackoverflow
Solution 5 - CDorotheaView Answer on Stackoverflow