简体   繁体   English

C中的回车?

[英]Carriage return in C?

Output of Following program is : hai 以下程序的输出是: hai

I didn't get how the \\r carriage return works in this program and in real can any one help me out ? 我没有得到\\r回车在这个程序中是如何工作的,在真实的任何人都可以帮助我吗?

#include <stdio.h>
#include<conio.h>

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

From 5.2.2/2 (character display semantics) : 从5.2.2 / 2(字符显示语义):

\\b ( backspace ) Moves the active position to the previous position on the current line. \\b退格键 )将活动位置移动到当前行的上一个位置。 If the active position is at the initial position of a line, the behavior of the display device is unspecified. 如果活动位置在线的初始位置,则不指定显示设备的行为。

\\n ( new line ) Moves the active position to the initial position of the next line. \\n新行 )将活动位置移动到下一行的初始位置。

\\r ( carriage return ) Moves the active position to the initial position of the current line. \\r回车 )将活动位置移动到当前行的初始位置。

Here, your code produces : 在这里,您的代码生成:

  • <new_line>ab
  • \\b : back one character \\b :支持一个字符
  • write si : overrides the b with s (producing asi on the second line) si :用s覆盖b (在第二行产生asi
  • \\r : back at the beginning of the current line \\r :回到当前行的开头
  • write ha : overrides the first two characters (producing hai on the second line) ha :覆盖前两个字符(在第二行产生hai

In the end, the output is : 最后,输出是:

\nhai

Program prints ab , goes back one character and prints si overwriting the b resulting asi . 程序打印ab ,返回一个字符并打印si覆盖b得到的asi Carriage return returns the caret to the first column of the current line. 回车符将插入符号返回到当前行的第一列。 That means the ha will be printed over as and the result is hai 这意味着ha将打印在as其结果是hai

Step-by-step: 一步步:

[newline]ab [换行符] AB

ab

[backspace]si [退格] SI

asi

[carriage-return]ha [回车]公顷

hai

Carriage return, does not cause a newline. 回车,不会造成换行。 Under some circumstances a single CR or LF may be translated to a CR-LF pair. 在某些情况下,单个CR或LF可以转换为CR-LF对。 This is console and/or stream dependent. 这是依赖于控制台和/或流的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM