Why and how do the following carriage return results appear?

#include <stdio.h>

void main()
{
    printf("ab");
    printf("\bsi");
    printf("\rha");
}

this code gives the result "ha" in the GCC 4.8 compiler

#include <stdio.h>

void main()
{
    printf("ab");
    printf("\bsi");
    printf("\rha");
    printf("\n");
}

this code gives the output of "hai" in the GCC 4.8 compiler

Now the question arises as to why the output changes from "ha" to "hai" only when adding the expression printf ("\ n"); at the end, which (for me) should not affect the code due to previous lines.

+4
source share
3 answers

, , , . , , \rha, i. i .

\n , , .

+5

:

  • \n:: .
  • \b:: . .
  • \r:: . .

, printf: 1. "ab", . 2. "asi" (\ b), . 3. "hai", ha, i.

, OUTPUT:: hai

"i" - , -

+2

If you compile the first code, instead of i there is a cursor so that you cannot see that i. In the second code, the cursor is on a new line and does not apply to any character.

0
source

All Articles