What's the Use of '\r' escape sequence?
I have C code like this:
#include<stdio.h>
int main()
{
printf("Hey this is my first hello world \r");
return 0;
}
I have used the \r
escape sequence as an experiment. When I run the code I get the output as:
o world
Why is that, and what is the use of \r
exactly?
If I run the same code in an online compiler I get the output as:
Hey this is my first hello world
Why did the online compiler produce different output, ignoring the \r
?
\r
is a carriage return character; it tells your terminal emulator to move the cursor at the start of the line.
The cursor is the position where the next characters will be rendered.
So, printing a \r
allows to override the current line of the terminal emulator.
Tom Zych figured why the output of your program is o world
while the \r
is at the end of the line and you don't print anything after that:
When your program exits, the shell prints the command prompt. The terminal renders it where you left the cursor. Your program leaves the cursor at the start of the line, so the command prompt partly overrides the line you printed. This explains why you seen your command prompt followed by o world
.
The online compiler you mention just prints the raw output to the browser. The browser ignores control characters, so the \r
has no effect.
See https://en.wikipedia.org/wiki/Carriage_return
Here is a usage example of \r
:
#include <stdio.h>
#include <unistd.h>
int main()
{
char chars[] = {'-', '\\', '|', '/'};
unsigned int i;
for (i = 0; ; ++i) {
printf("%c\r", chars[i % sizeof(chars)]);
fflush(stdout);
usleep(200000);
}
return 0;
}
It repeatedly prints the characters -
\
|
/
at the same position to give the illusion of a rotating |
in the terminal.
The program is printing "Hey this is my first hello world "
, then it is moving the cursor back to the beginning of the line. How this will look on the screen depends on your environment. It appears the beginning of the string is being overwritten by something, perhaps your command line prompt.
The '\r' stands for "Carriage Return" - it's a holdover from the days of typewriters and really old printers. The best example is in Windows and other DOSsy OSes, where a newline is given as "\r\n". These are the instructions sent to an old printer to start a new line: first move the print head back to the beginning, then go down one.
Different OSes will use other newline sequences. Linux and OSX just use '\n'. Older Mac OSes just use '\r'. Wikipedia has a more complete list, but those are the important ones.
Hope this helps!
PS: As for why you get that weird output... Perhaps the console is moving the "cursor" back to the beginning of the line, and then overwriting the first bit with spaces or summat.
\r
move the cursor to the begin of the line.
Line breaks are managed differently on different systems. Some only use \n
(line feed, e.g. Unix), some use (\r
e.g. MacOS before OS X afaik) and some use \r\n
(e.g. Windows afaik).
As amaud576875 said, the \r
escape sequence signifies a carriage-return, similar to pressing the Enter key. However, I'm not sure how you get "o world"; you should (and I do) get "my first hello world" and then a new line. Depending on what operating system you're using (I'm using Mac) you might want to use a \n
instead of a \r
.