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 ?
#include <stdio.h>
#include<conio.h>
void main()
{
printf("\nab");
printf("\bsi");
printf("\rha");
_getch();
}
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 si
: overrides the b
with s
(producing asi
on the second line) \\r
: back at the beginning of the current line ha
: overrides the first two characters (producing hai
on the second line) In the end, the output is :
\nhai
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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.