4

I am a beginner in C programming language, recently I have studied about escape sequence.

\n means newline
\b means backspace
\r means carriage-return

When I am applying these on the following program, then I am getting output as hai, Can anyone please explain, how?

main()
{
    printf("\nab");
    printf("\bsi");
    printf("\rha");
}
Werner Henze
  • 16,404
  • 12
  • 44
  • 69
Mayank Tiwari
  • 2,974
  • 5
  • 30
  • 52
  • 1
    Your assumption may not be true, and the output may varies, see [Usage of \b and \r in C](http://stackoverflow.com/q/17236242/1009479) – Yu Hao Aug 07 '13 at 08:48

7 Answers7

13

\r is actually carriage return (which takes the cursor to the start of the line).

Your program outputs a new line (\n) followed by "ab" then backspace (\b) (over the b) "si", so you now have "asi" on the screen.
The \r takes the cursor to the start of the line and then outputs "ha" leaving "hai" on the screen.

Werner Henze
  • 16,404
  • 12
  • 44
  • 69
noz
  • 1,863
  • 13
  • 14
5

The first instruction will print ab on a new line (\n) :

>ab

The second instruction will make a backspace (\b) before printing si :

>asi

Then the last one will make a carriage return (\r) before printing ha :

>hai
zakinster
  • 10,508
  • 1
  • 41
  • 52
4

First printf will print ab then \b will make backspace and print si so your final print becomes asi.

Now in last \r will move the cursor at beginning and prints ha which will replace first two character and makes final string as hai(ha from last statement and i from earlier result.)

Dayal rai
  • 6,548
  • 22
  • 29
3

Because you are writing everything on the same line after the first \n :

 printf( "\nab" ); // -> ab on a new line

Line status : ab

 printf( "\bsi" );

It means ab minus the b because of the backspace then you add si to the line.

Line status : asi

Finally :

 printf( "\rha" );

Means that you go back at the beginning of the line and write ha.

It leads to hai.


\r is carriage-return, not linefeed.

Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62
2

No, '\r' is not linefeed, '\n' is actually linefeed. It just happens to be so that many modern systems uses '\n' as both linefeed and carriage-return, making it "newline".

'\r' is carriage-return, which returns the cursor to the start of the line.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

\b is for backspace. That is, it removes the character just written.
\r is to return the cursor back to the left most position in the current line.


Let z represent cursor.
After statement one.

\n
abz

After backspace.

az

After statement two.

asiz

After return.

zbi

After statement three

haiz

Bhargav Ponnapalli
  • 9,224
  • 7
  • 36
  • 45
0

Here in your code

  1. \r

    \r means Carriage Return, which means: return the cursor to the beginning of the line in more simple words we can say that it's deleting each character from the active position upto the beginning.
    
  2. \n

    \n mean New Line
    
  3. \b

    \b mean Backspace
    

Let's take it one step at a time:

<new line>ab<backspace>si<carriage return>ha

First, handle the backspace. Note that even though it is "non-erase", the next character to be output would overwrite what was backspaced over:

<new line>asi<carriage return>ha

Now, a carriage return means to go back to the beginning of the line. So the "ha" overwrites the "as" in "asi:

<new line>hai

Now, the cursor is currently sitting on the i, so the next character to be output would overwrite i.

Rahul Kulhari
  • 1,115
  • 1
  • 15
  • 44