1

它总是显示“你好世界”。为什么?

#include <stdio.h>

int main(void)
{
    printf("..... world\rhello\n");
    return 0;
}
4

5 回答 5

10

这是因为\r回车符(CR)。它将插入符号返回到行首。然后你hello在那里写,有效地覆盖了这些点。

\n另一方面,(换行符,LF)用于将插入符号向下移动一行,这就是为什么电传打字机具有序列 CR-LF 或回车后跟换行符以将插入符号定位在下一行的开头. Unix 取消了这一点,而 LF 现在自行完成了。不过,CR 仍然以其旧的语义存在。

于 2013-08-28T09:01:59.713 回答
4

使用\r您将返回到当前行的开头并覆盖点“ .....”:

printf("..... world\rhello\n");
        ^^^^^        vvvvv
        hello <----- hello

这个怎么运作:

..... world
           ^

然后返回到当前行的开头:

..... world
^

然后在 . 之后打印一个单词\r。结果是:

hello world
           ^
于 2013-08-28T09:04:23.223 回答
2

因为唯一的\r回车)字符导致您的终端返回到行首,而不更改行。因此, 左边的字符\r被 覆盖"hello"

于 2013-08-28T09:02:05.440 回答
0
#include<stdio.h>
#include<conio.h>
int main(void) 

{   
    // You will hear Audible tone 3 times.
    printf("The Audible Bell --->           \a\a\a\n");
    // \b (backspace) Moves the active position to the 
    // previous position on the current line. 
    printf("The Backspace --->              ___ \b\b\b\b\b\b\b\b\b\bTesting\n");
    //\n (new line) Moves the active position to the initial 
    // position of the next line.
    printf("The newline ---> \n\n");
    //\r (carriage return) Moves the active position to the 
    // initial position of the current line.
    printf("The carriage return --->        \rTesting\rThis program is for testing\n");
    // Moves the current position to a tab space position
    printf("The horizontal tab --->         \tTesting\t\n");

    getch();
    return 0;
}

/***************************OUTPUT************************
The Audible Bell --->
The Backspace --->                        Testing__
The newline --->

This program is for testing
The horizontal tab --->                         Testing
***************************OUTPUT************************/
于 2013-08-28T09:21:07.327 回答
0

再次检查它,它会给出 put like

..... world
hello

以及您在 printf() 中写的内容,它将作为输出返回

于 2013-08-28T09:10:28.770 回答