需要帮助理解\\ n \\ b和\\ R将如何​​渲染输出的printf [英] Need help understanding how \n, \b, and \r will render printf output

查看:88
本文介绍了需要帮助理解\\ n \\ b和\\ R将如何​​渲染输出的printf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在C下面的程序,当我运行它,我被看的输出感到惊讶。

I wrote the following program in the C and when I run it, I was surprised by looking at the output.

下面是节目

int main()
{    
       printf("\nab");
       printf("\bsi");    
       printf("\rha");    
}

输出是: - 海
而我期待absiha,因为\\ n是新行,\\ b是退格(非擦除)和\\ r表示回车。所以我期待的柯森是在我字,因为\\ R已经被应用,但是当我运行它,看到输出我完全感到惊讶和困惑。任何人都可以请解释一下我的输出?

The output is :- hai whereas I was expecting "absiha" since \n is for new line, \b is for backspace(non erase) and \r is for carriage return. So I was expecting that curson would be at "i" character because \r has been applied but when I run it and saw the output I was totally surprised and confused. Can anyone please explain me the output?

推荐答案

让我们把它一步步时间:

Let's take it one step at a time:

<新行> AB<退格>硅<回车>哈

首先,办理退格。注意,即使它是非擦除,下一个字符被输出将覆盖什么退格以上:

First, handle the backspace. Note that even though it is "non-erase", the next character to be output would overwrite what was backspaced over:

<新行> ASI<回车>哈

现在,回车装置返回该行的开头。因此,哈覆盖为ASI:

Now, a carriage return means to go back to the beginning of the line. So the "ha" overwrites the "as" in "asi:

<新行>海

现在,游标当前坐在 I ,所以接下来的字符输出会覆盖 I

Now, the cursor is currently sitting on the i, so the next character to be output would overwrite i.

这篇关于需要帮助理解\\ n \\ b和\\ R将如何​​渲染输出的printf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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