#include <stdio.h>
void main()
{
printf("ab");
printf("\bsi");
printf("\rha");
}
this code gives the output of "ha" on GCC 4.8 compiler
#include <stdio.h>
void main()
{
printf("ab");
printf("\bsi");
printf("\rha");
printf("\n");
}
this code gives the output of "hai" on GCC 4.8 compiler
now the question is why does the output change from "ha" to "hai" just on adding the statement printf("\n"); at the end which (according to me) should not affect the code due to the preceding lines.
When your program ends, the shell writes the prompt starting at whatever position the cursor was last on. So in the first case, after \rha
, the cursor is sitting on the i
. The shell will overwrite the i
with whatever the first character of your prompt is.
In the second case, you output a \n
at the end which moves the cursor to the next line, where the shell writes its prompt.
First of all you need to understand the white space characters:
So result of printf statments are:: 1. Prints "ab" , cursor sitting at end of line. 2. Prints "asi" after moving the cursor back one space (\b), cursor sitting at the end of a line. 3. Prints "hai", cursor sitting after ha, just below i.
So, OUTPUT :: hai
In first case you are not able to see 'i' because of the cursor whereas in second due to newline character you are able to see it
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