Carriage return in C?

前端 未结 3 1490
执念已碎
执念已碎 2020-12-07 16:43

Output of Following program is : hai

I didn\'t get how the \\r carriage return works in this program and in real can any one help me out

3条回答
  •  天涯浪人
    2020-12-07 17:25

    From 5.2.2/2 (character display semantics) :

    \b (backspace) Moves the active position to the previous position on the current line. If the active position is at the initial position of a line, the behavior of the display device is unspecified.

    \n (new line) Moves the active position to the initial position of the next line.

    \r (carriage return) Moves the active position to the initial position of the current line.

    Here, your code produces :

    • ab
    • \b : back one character
    • write si : overrides the b with s (producing asi on the second line)
    • \r : back at the beginning of the current line
    • write ha : overrides the first two characters (producing hai on the second line)

    In the end, the output is :

    \nhai
    

提交回复
热议问题