Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the '\b' at the end of the string doesn't have effect? [duplicate]

Tags:

c

backspace

Here is the code below:

#include <stdio.h>

int main(int argc, char* argv[])
{
    printf("WORD\b\b WORD\b\b");
    return 0;
}

which generates this output:

WO WORD

The question is why the last \b does not have effect on the second word more specifically when they are at the end of the string?

like image 316
Meninx - メネンックス Avatar asked Jul 26 '16 12:07

Meninx - メネンックス


2 Answers

It does have an affect, the affect is moving the cursor back, but '\b' won't delete any characters unless you overwrite them.

in case you want to print something else afterwards, printing will resume from the current cursor position.

note: this behavior is dependent on the terminal you use to display text.

like image 142
monkeyStix Avatar answered Nov 15 '22 07:11

monkeyStix


This depends mainly on the shell / terminal you're using and how it interprets backspace characters.

The behavior you describe above occurs on Windows in a Command Prompt. This terminal apparently moves the cursor back one space on a backspace but does not delete the character. Any characters printed after the backspace overwrite previously written characters.

For example, if you were to do this:

printf("WORD\b\b WORD\b\bx");

Your output would be this:

WO WOxD

In contrast, running your code on a Ubuntu machine under bash results in the following output:

WO WO
like image 30
dbush Avatar answered Nov 15 '22 09:11

dbush