什么是'\\ R'转义序列的使用? [英] What's the Use of '\r' escape sequence?

查看:172
本文介绍了什么是'\\ R'转义序列的使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C code是这样的:

I have C code like this:

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

我已经使用了 \\ r 转义序列作为一个实验。当我运行code我得到的输出:

I have used the \r escape sequence as an experiment. When I run the code I get the output as:

o world

这是为什么,什么是使用 \\ r 的是什么呢?

如果我在网上编译运行相同的code我得到的输出:

If I run the same code in an online compiler I get the output as:

Hey this is my first hello world

为什么网上编译器产生不同的输出,忽略了 \\ r

推荐答案

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

所以,印刷 \\ r 允许覆盖终端仿真器的当前行。

So, printing a \r allows to override the current line of the terminal emulator.

<一个href=\"http://stackoverflow.com/questions/7372918/whats-the-use-of-r-escape-sequence/7372959#7372959\">Tom齐赫想通,为什么你的程序的输出是 0世界国际,而 \\ r 在年底行了,你后不打印任何东西:

Tom Zych figured why the output of your program is o wolrd while the \r is at the end of the line and you don't print anything after that:

当你的程序退出时,shell打印命令提示符。终端使得它在你离开光标。你的程序离开光标在该行的开始,所以在命令提示符下部分会覆盖打印的行。这就解释了为什么你看过之后命令提示符Ø世界

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.

在线编译器的你提到刚刚打印的原始输出到浏览器。该浏览器会忽略控制字符,因此 \\ r 没有效果。

The online compiler you mention just prints the raw output to the browser. The browser ignores control characters, so the \r has no effect.

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

下面是一个使用示例 \\ 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.

这篇关于什么是'\\ R'转义序列的使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆