+ 2
Why the output is "ha i" in this code and also what changes are occur at each step of execution?
#include<stdio.h> int main( ) { printf("\n ks"); printf("\b mi \a"); printf("\r ha \n"); return 0; }
3 Respuestas
+ 2
It's almost the same as `printf ("\n ks\b mi \a\r ha \n");` and actually prints out "ha i" not "hai", or precisely,
Output:
ha i
First, it executes \n which is an escape sequence for a new line, and then it prints " ks" but not directly to the stdout since you don't have a pauser (something like getchar, getch, system("pause")). Then \b (which is an escape sequence that move your "cursor" backwards one time),
Before \b:
ks| // '|' is the cursor or caret block
After \b:
k|s // the cursor moved backwards
After that it prints out " mi " and the ' ' (space character) replaces 's' (it becomes " k mi " now). I'll ignore \a since it's only a beep or alarm, now \r is carriage return, it moves your cursor to the beginning of the current line ("| k mi "). From the cursor, it prints out " ha \n" (replaces the space with space, k with h, space with a, m with space), note that "i " stays because it's untouched, therefore, \n printed out after spaces next to i to stdout (see how stack works to understand).
+ 4
Depends on the terminal configurations \a and \b will not have the some behavior.
+ 1
Process: (I use `^` as the cursor)
" ks^"
" k^s"
" k mi ^"
"^ k mi "
" ha i "
The point is, printf processes the const char* inside it before putting it to stdout.