Carriage return in C?

前端 未结 3 1485
执念已碎
执念已碎 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:24

    Step-by-step:

    [newline]ab

    ab
    

    [backspace]si

    asi
    

    [carriage-return]ha

    hai
    

    Carriage return, does not cause a newline. Under some circumstances a single CR or LF may be translated to a CR-LF pair. This is console and/or stream dependent.

    0 讨论(0)
  • 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 :

    • <new_line>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
    
    0 讨论(0)
  • 2020-12-07 17:26

    Program prints ab, goes back one character and prints si overwriting the b resulting asi. Carriage return returns the caret to the first column of the current line. That means the ha will be printed over as and the result is hai

    0 讨论(0)
提交回复
热议问题