在"退格"逃生用C字符'\\ B',意外的行为? [英] The "backspace" escape character '\b' in C, unexpected behavior?

查看:106
本文介绍了在"退格"逃生用C字符'\\ B',意外的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我终于通读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

有人能解释一下吗?

Can anyone explain this?

推荐答案

您的结果将取决于什么样的你在终端或控制台程序有所不同,但没错,在大多数 \\ b 无损的退格。它向后移动光标,但不会抹掉那里的东西。

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.

因此​​,对于你好WORL 的一部分,code输出

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
        ^

请注意现在光标在研究。然后,它输出 D ,这将覆盖研究,给了我们:

Note the cursor is now on the r. Then it outputs d, which overwrites the r and gives us:


hello wodl
         ^

最后,它输出 \\ n ,它是一种非破坏性的换行(再次,在大多数终端,包括显然你的),因此保持不变,并且光标移动到下一行的开头。

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.

这篇关于在&QUOT;退格&QUOT;逃生用C字符'\\ B',意外的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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