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?
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With