Reputation: 13811
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
?
Upvotes: 93
Views: 269301
Reputation: 99911
\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.
Upvotes: 148
Reputation: 14619
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.
Upvotes: 15
Reputation: 30911
To answer the part of your question,
what is the use of
\r
?
Many Internet protocols, such as FTP, HTTP and SMTP, are specified in terms of lines delimited by carriage return and newline. So, for example, when sending an email, you might have code such as:
fprintf(socket, "RCPT TO: %s\r\n", recipients);
Or, when a FTP server replies with a permission-denied error:
fprintf(client, "550 Permission denied\r\n");
Upvotes: 2
Reputation: 20640
This is from antiquated technology: The old fashion typewriter style of printer. There was a roller (platen) that advanced the paper and a print head that hammered a metal key against an ink fabric.
\r Return the print head to the left side.
\n Advance the platen one line.
If the \n was not issued, you would type over what was on a line (used mostly for underlining text).
Upvotes: 2
Reputation: 176
\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).
Upvotes: 7
Reputation: 28872
It is quite useful, when you are running on the unix platform, and need to create a text file which will be opened on the dos platform.
Unix uses '\n' as its line terminator, and dos uses '\r\n' as its line terminator, so you can use it to create a dos text file.
Upvotes: 1
Reputation: 16047
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
.
Upvotes: 2
Reputation: 13586
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.
Upvotes: 29