4
#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.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
aroonav
  • 85
  • 7
  • There's no difference in output if you compare in Ideone: [sample1](http://ideone.com/YRHEgp), [sample2](http://ideone.com/exlshK). It's a behavior of the particular terminal/shell implementation. – πάντα ῥεῖ Dec 09 '13 at 20:55
  • ok but i would like to know how does it differ in a shell from ideone. – aroonav Dec 09 '13 at 21:03

3 Answers3

5

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.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • i got the reason for the first output but in the second output after printf("\rha") "hai" would be present and the cursor would be at the end of the second character ready to write the third character...then printf("\n") would execute....so should the third character 'i' be removed and the cursor move onto the next line(giving output "ha") or will the character 'i' remain intact and the cursor move to the next line((giving output "hai"))?? seeing the output i guess its the former.. – aroonav Dec 09 '13 at 21:01
  • @sparx: Yes, the `\n` moves to the next line without changing anything on the current line. So if the cursor is sitting on the `i` when you output the `\n`, the `i` remains in place. – Greg Hewgill Dec 09 '13 at 21:04
2

First of all you need to understand the white space characters:

  1. \n :: It moves the cursor to the next line.
  2. \b :: It moves the cursor one character back to the left of Console. Just simply backspaces one character.
  3. \r :: Carrage Returns.It moves cursor to extreme right of the same line.

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

Vikas Sangle
  • 632
  • 5
  • 19
0

If you compile the first code, in the place of i, there is the cursor so you can't see that i. In the second code, the cursor is in a new line and it doesn't cover any character.

enedil
  • 1,605
  • 4
  • 18
  • 34