\\ b和\\ r在C用法 [英] Usage of \b and \r in C

查看:143
本文介绍了\\ b和\\ r在C用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

\\ b \\ r 在实践中很少使用。我刚刚发现我误会了这两个转义序列。一个简单的测试:

 的printf(富\\ BBAR \\ n);

我,它预计到输出 fobar ,因为 \\ b 将退光标,和将覆盖第二个 0 ,而是它输出: foobar的

同样是 \\ r

 的printf(富\\ RBAR \\ n);

我觉得 \\ r 将光标移动到当前行的开始,所以将取代,所以最终的输出应。但是,它实际上输出:

 
酒吧


解决方案

中的人物将获得派就这样底层输出设备(你的情况可能是一个终端仿真器)。

这是由终端的执行这些字符获取那么如何实际显示。例如,一个钟( \\ A )可能会引发一些终端嘟嘟声,屏幕上别人闪光灯,否则会被完全忽略。这一切都取决于终端的配置方式。

\b and \r are rarely used in practice. I just found out that I misunderstood these two escape sequences. A simple test:

printf("foo\bbar\n");

I expected it to output fobar, because \b will backspace the cursor, and b will overwrite the second o, but instead it outputs: foobar

The same is with \r:

printf("foo\rbar\n");

I thought \r will move the cursor to the beginning of the current line, so bar will replace foo, so the final output should be bar. However, it actually outputs:

foo
bar

解决方案

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.

这篇关于\\ b和\\ r在C用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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