C语言 在 C 中使用 \b 和 \r
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17236242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Usage of \b and \r in C
提问by Yu Hao
\band \rare rarely used in practice. I just found out that I misunderstood these two escape sequences. A simple test:
\b并且\r在实践中很少使用。我刚刚发现我误解了这两个转义序列。一个简单的测试:
printf("foo\bbar\n");
I expected it to output fobar, because \bwill backspace the cursor, and bwill overwrite the second o, but instead it outputs: foobar
我希望它输出fobar,因为\b会退格光标,并b覆盖第二个o,但它输出:foobar
The same is with \r:
同样是\r:
printf("foo\rbar\n");
I thought \rwill move the cursor to the beginning of the current line, so barwill replace foo, so the final output should be bar. However, it actually outputs:
我以为\r会将光标移动到当前行的开头,所以bar会替换foo,所以最终输出应该是bar. 但是,它实际上输出:
foo
bar
回答by ComicSansMS
The characters will get send just like that to the underlying output device (in your case probably a terminal emulator).
字符将像那样发送到底层输出设备(在您的情况下可能是终端模拟器)。
It is up to the terminal's implementation then how those characters get actually displayed. For example, a bell (\a) could trigger a beep sound on some terminals, a flash of the screen on others, or it will be completely ignored. It all depends on how the terminal is configured.
这些字符如何实际显示取决于终端的实现。例如,铃声 ( \a) 可能会在某些终端上触发哔声,在其他终端上触发屏幕闪烁,或者它会被完全忽略。这完全取决于终端的配置方式。
回答by Mark Ransom
The characters are exactly as documented - \bequates to a character code of 0x08and \requates to 0x0d. The thing that varies is how your OS reacts to those characters. Back when displays were trying to emulate an old teletype those actions were standardized, but they are less useful in modern environments and compatibility is not guaranteed.
字符与文档完全相同 -\b等同于 的字符代码0x08和\r等同于0x0d。不同的是您的操作系统对这些字符的反应。当显示器试图模拟旧的电传打字机时,这些操作是标准化的,但它们在现代环境中不太有用,并且无法保证兼容性。
回答by Joni
The interpretation of the backspace and carriage return characters is left to the software you use for display. A terminal emulator, when displaying \b would move the cursor one step back, and when displaying \r to the beginning of the line. If you print these characters somewhere else, like a text file, the software may choose. to do something else.
退格和回车字符的解释留给您用于显示的软件。终端模拟器,当显示 \b 时会将光标向后移动一步,而当显示 \r 到行首时。如果您在其他地方打印这些字符,如文本文件,软件可能会选择。做别的事情。
回答by Moh K
I have experimented many of the backslash escape characters. \nwhich is a new line feed can be put anywhere to bring the effect. One important thing to remember while using this character is that the operating system of the machine we are using might affect the output. As an example, I have printed a bunch of escape character and displayed the result as follow to proof that the OS will affect the output.
我已经试验了许多反斜杠转义字符。\n这是一个新的换行符,可以放在任何地方带来效果。使用此字符时要记住的一件重要事情是我们使用的机器的操作系统可能会影响输出。例如,我打印了一堆转义字符并显示结果如下,以证明操作系统会影响输出。
Code:
代码:
#include <stdio.h>
int main(void){
printf("Hello World!");
printf("Goodbye \a");
printf("Hi \b");
printf("Yo\f");
printf("What? \t");
printf("pewpew");
return 0;
}
回答by Hymany Wang
As for the meaning of each character described in C Primer Plus, what you expected is an 'correct' answer. It should be true for some computer architectures and compilers, but unfortunately not yours.
至于C Primer Plus中描述的每个字符的含义,您期望的是“正确”的答案。对于某些计算机体系结构和编译器来说应该是正确的,但不幸的是不是你的。
I wrote a simple c program to repeat your test, and got that 'correct' answer. I was using Mac OS and gcc.

我写了一个简单的 c 程序来重复你的测试,并得到了“正确”的答案。我使用的是 Mac OS 和 gcc。

Also, I am very curious what is the compiler that you were using. :)
另外,我很好奇您使用的编译器是什么。:)

