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.
No, the output from your program to the kernel is line-buffered. That's the default behaviour for stdio when stdout is a terminal. Add the call setbuf(stdout, NULL) to turn output buffering off for stdout. See setbuf(3).
- Since the
\b\b goes back two spaces, to the position of l then similar to how l was replaced by h, the o should have been replaced by \n. Why wasn't it?
Because the newline character just moves the cursor (and scrolls the screen), it doesn't print as a visible character that would take up the place of a glyph on the terminal. If we assume it would take the place of a glyph, what would it look like?
- if I input something into the very same program, and press Backspace, it erases the last character, but not for the output. Why?
Well, what happens when you type depends on what mode the terminal is in. Roughly, it can be in the usual mode where the terminal itself provides elementary line editing (handles backspaces); or in a "raw" mode, where all keypresses go to the application, and it's up to the application to decide what to do with them, and what to output in response.
If you run e.g. cat, the terminal will likely handle the line editing, and hitting for example x, <backspace>, Ctrl-D will result in cat just reading the empty input, signalling the end of input . You can check this with strace. But if you run an interactive Bash shell instead, it will handle the backspace by itself, and output what it considers appropriate to do what the user expects, i.e. wipe one character.
Run: strace -etrace=read,write -obash.trace bash, and enter
x<backspace><Ctrl-D>. Now bash.trace will show these system calls:
read(0, "x", 1) = 1
write(2, "x", 1) = 1
read(0, "\177", 1) = 1
write(2, "\10\33[K", 4) = 4
read(0, "\4", 1) = 1
First, Bash reads and writes the x, then it reads the backspace (character code 0177 in octal or 127 in decimal), and outputs the backspace (octal 10, decimal 8) which moves the cursor back and outputs the control sequence for clearing the end of the line, <ESC>[K. The last \4 is the Ctrl-D.