'\r' 有什么用?转义序列?

发布于 2024-12-04 02:29:20 字数 415 浏览 2 评论 0原文

我有这样的 C 代码:

#include<stdio.h>
int main()
{
    printf("Hey this is my first hello world \r");
    return 0;
}

我使用 \r 转义序列作为实验。当我运行代码时,我得到的输出为:

o world

为什么会这样, \r 到底有什么用?

如果我在在线编译器中运行相同的代码,我得到的输出为:

Hey this is my first hello world

为什么在线编译器会产生不同的输出,忽略 \r

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

空心空情空意 2024-12-11 02:29:20

\r 是一个回车字符;它告诉您的终端模拟器将光标移动到行的开头。

光标是渲染下一个字符的位置。

因此,打印 \r 允许覆盖终端模拟器的当前行。

Tom Zych 弄清楚为什么程序的输出是o world\r 位于行尾并且之后不打印任何内容:

当程序退出时,shell打印命令提示符。终端将其呈现在您离开光标的位置。您的程序将光标保留在行的开头,因此命令提示符会部分覆盖您打印的行。这解释了为什么您会看到命令提示符后面跟着 o world

您提到的在线编译器只是将原始输出打印到浏览器。浏览器会忽略控制字符,因此 \r 不起作用。

请参阅 https://en.wikipedia.org/wiki/Carriage_return

这是 < 的使用示例code>\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;
}

在同一位置重复打印字符- \ | /给人一种旋转的错觉| 在终端中。

\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.

流云如水 2024-12-11 02:29:20

该程序正在打印“Hey this is my first hello world”,然后将光标移回行首。屏幕上的显示效果取决于您的环境。字符串的开头似乎被某些东西覆盖,也许是你的命令行提示符。

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.

自我难过 2024-12-11 02:29:20

'\r' 代表“回车” - 这是打字机和非常旧的打印机时代的遗留物。最好的例子是在 Windows 和其他 DOSsy 操作系统中,其中换行符表示为“\r\n”。这些是发送到旧打印机以开始新行的指令:首先将打印头移回开头,然后向下移动一个。

不同的操作系统将使用其他换行序列。 Linux 和 OSX 仅使用“\n”。较旧的 Mac 操作系统仅使用“\r”。 维基百科有一个更完整的列表,但这些是重要的。

希望这有帮助!

PS:至于为什么你会得到那个奇怪的输出......也许控制台正在将“光标”移回到行的开头,然后用空格或总和覆盖第一位。

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.

无法言说的痛 2024-12-11 02:29:20

\r 将光标移动到行首。

不同系统上换行符的管理方式不同。有些只使用 \n (换行,例如 Unix),有些使用(\r 例如 OS X 之前的 MacOS afaik),有些使用 \r\n< /code> (例如Windows faik)。

\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).

吃颗糖壮壮胆 2024-12-11 02:29:20

正如 amaud576875 所说,\r 转义序列表示回车,类似于按 Enter 键。然而,我不确定你是如何得到“o world”的;你应该(我也确实)得到“我的第一个你好世界”,然后是一个新行。根据您使用的操作系统(我使用的是 Mac),您可能需要使用 \n 而不是 \r

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.

情话墙 2024-12-11 02:29:20

这是来自过时的技术:老式打字机风格的打印机。有一个滚筒(压板)可以推进纸张,还有一个打印头可以用金属键敲击墨水织物。

\r 将打印头返回到左侧。

\n 将压板前进一行。

如果未发出 \n,您将键入一行上的内容(主要用于下划线文本)。

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).

还在原地等你 2024-12-11 02:29:20

为了回答你的问题的一部分,

\r有什么用?

许多 Internet 协议(例如 FTP、HTTP 和 SMTP)都是按照由回车符和换行符分隔的行来指定的。因此,例如,在发送电子邮件时,您可能有如下代码:

fprintf(socket, "RCPT TO: %s\r\n", recipients);

或者,当 FTP 服务器回复权限拒绝错误时:

fprintf(client, "550 Permission denied\r\n");

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");
暮倦 2024-12-11 02:29:20

当你运行在unix平台上,并且需要创建一个文本文件时,它非常有用
它将在dos平台上打开。

Unix 使用“\n”作为行终止符,dos 使用“\r\n”作为行终止符,因此您可以使用它来创建 dos 文本文件。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文