C语言 “退格”转义字符 '\b':意外行为?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6792812/
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
The "backspace" escape character '\b': unexpected behavior?
提问by OregonTrail
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.
所以我终于通读了K&R,我在前几页中学到了一些东西,有一个退格转义字符,\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");
}
The output is
输出是
hello wodl
Can anyone explain this?
谁能解释一下?
回答by T.J. Crowder
Your result will vary depending on what kind of terminal or console program you're on, but yes, on most \bis a nondestructivebackspace. It moves the cursor backward, but doesn't erase what's there.
您的结果将根据您使用的终端或控制台程序的类型而有所不同,但是是的,大多数\b是非破坏性退格。它向后移动光标,但不会删除那里的内容。
So for the hello worlpart, the code outputs
所以对于这hello worl部分,代码输出
hello worl
^
...(where ^shows where the cursor is) Then it outputs two \bcharacters which moves the cursor backward two places withouterasing (on your terminal):
...(其中^显示光标所在的位置)然后它输出两个\b字符,将光标向后移动两个位置而不擦除(在您的终端上):
hello worl
^
Note the cursor is now on the r. Then it outputs d, which overwrites the rand gives us:
请注意,光标现在位于r. 然后它输出d,它覆盖了r并给了我们:
hello wodl
^
Finally, it outputs \n, which is a non-destructive newline (again, on most terminals, including apparently yours), so the lis left unchanged and the cursor is moved to the beginning of the next line.
最后,它输出\n,这是一个非破坏性的换行符(同样,在大多数终端上,显然包括你的终端),所以l保持不变,光标移动到下一行的开头。
回答by pmg
.......... ^ <= pointer to "print head"
/* part1 */
printf("hello worl");
hello worl
^ <= pointer to "print head"
/* part2 */
printf("\b");
hello worl
^ <= pointer to "print head"
/* part3 */
printf("\b");
hello worl
^ <= pointer to "print head"
/* part4 */
printf("d\n");
hello wodl ^ <= pointer to "print head" on the next line
回答by Peter K.
If you want a destructive backspace, you'll need something like
如果你想要一个破坏性的退格,你需要像
"\b \b"
i.e. a backspace, a space, and another backspace.
即退格、空格和另一个退格。
回答by Nemo
Not too hard to explain... This is like typing hello worl, hitting the left-arrow key twice, typing d, and hitting the down-arrow key.
不太难解释...这就像键入hello worl,按两次向左箭头键,键入d,然后按向下箭头键。
At least, that is how I infer your terminal is interpeting the \band \ncodes.
至少,这就是我推断您的终端正在插入\b和\n代码的方式。
Redirect the output to a file and I bet you get something else entirely. Although you may have to look at the file's bytes to see the difference.
将输出重定向到一个文件,我敢打赌你会得到完全不同的东西。尽管您可能需要查看文件的字节才能看到差异。
[edit]
[编辑]
To elaborate a bit, this printfemits a sequence of bytes: hello worl^H^Hd^J, where ^His ASCII character #8 and ^Jis ASCII character #10. What you see on your screen depends on how your terminal interprets those control codes.
详细说明一下,这printf会发出一个字节序列:hello worl^H^Hd^J,其中^H是 ASCII 字符 #8 和^JASCII 字符 #10。您在屏幕上看到的内容取决于您的终端如何解释这些控制代码。
回答by Dorothea
Use a single backspace after each character
printf("hello wor\bl\bd\n");
在每个字符后使用一个退格
printf("hello wor\bl\bd\n");

