This is about the behaviour of the backspace (
\b
) character.
I have the following C program:
int main() {
printf("Hello\b\b");
sleep(5);
printf("h\n");
return 0;
}
The output on my terminal is
Helho
with the cursor advancing to the first position of the following line.
First, the entire thing prints only after the 5 second sleep, so from that I deduced the output from the kernel to the terminal is line buffered.
So now, my questions are:
1. Since the \b\b
goes back two spaces, to the position of the (second) l
, then similar to how l
was replaced by h
, the o
should have been replaced by \n
. Why wasn't it?
2. If I remove the line printf("h\n");
, it prints Hello
and goes back two characters, without erasing. This I got from other answers is because of a non-destructive backspace. Why is this behaviour different for input and output? That is, if I input something into the terminal (even the very same program) and press Backspace, it erases the last character, but not for the output. Why?
I'm on an Ubuntu system on the xterm terminal using bash, if that helps.
Asked by forumulator
(213 rep)
Jan 1, 2018, 05:43 PM
Last activity: Apr 17, 2024, 11:32 AM
Last activity: Apr 17, 2024, 11:32 AM