“退格"按钮转义字符"\ b":意外行为? [英] The "backspace" escape character '\b': unexpected behavior?
问题描述
所以我终于读完了 K& R ,我在第一篇中学到了一些东西几页,其中有一个退格转义字符\b
.
So I'm finally reading through K&R, and I learned something within the first few pages, that there is a backspace escape character, \b
.
所以我去测试一下,并且有一些非常奇怪的行为:
So I go to test it out, and there is some very odd behavior:
#include <stdio.h>
main ()
{
printf("hello worl\b\bd\n");
}
输出为
hello wodl
有人可以解释吗?
推荐答案
根据您所使用的终端程序或控制台程序的类型,您的结果将有所不同,但是可以,大多数\b
是 nondestructive 退格键.它将光标向后移动,但不会擦除其中的内容.
Your result will vary depending on what kind of terminal or console program you're on, but yes, on most \b
is a nondestructive backspace. It moves the cursor backward, but doesn't erase what's there.
因此对于hello worl
部分,代码将输出
So for the hello worl
part, the code outputs
hello worl
^
...(其中^
显示光标所在的位置),然后输出两个\b
字符,它们将光标向后移动两个位置而不会擦除(在您的终端上):
...(where ^
shows where the cursor is) Then it outputs two \b
characters which moves the cursor backward two places without erasing (on your terminal):
hello worl
^
请注意,光标现在位于r
上.然后输出d
,覆盖r
并给出:
Note the cursor is now on the r
. Then it outputs d
, which overwrites the r
and gives us:
hello wodl
^
最后,它输出\n
,这是非破坏性的换行符(同样,在大多数终端上,包括您的终端),因此l
保持不变,并且光标移至下一行的开头
Finally, it outputs \n
, which is a non-destructive newline (again, on most terminals, including apparently yours), so the l
is left unchanged and the cursor is moved to the beginning of the next line.
这篇关于“退格"按钮转义字符"\ b":意外行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!