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?
Your result will vary depending on what kind of terminal or console program you’re on, but yes, on most
\bis a nondestructive backspace. It moves the cursor backward, but doesn’t erase what’s there.So for the
hello worlpart, the code outputshello worl ^…(where
^shows where the cursor is) Then it outputs two\bcharacters which moves the cursor backward two places without erasing (on your terminal):hello worl ^Note the cursor is now on the
r. Then it outputsd, which overwrites therand gives us:hello wodl ^Finally, it outputs
\n, which is a non-destructive newline (again, on most terminals, including apparently yours), so thelis left unchanged and the cursor is moved to the beginning of the next line.