以下控制字符的含义:

回车 换行 换页


当前回答

Carriage return and line feed are also references to typewriters, in that the with a small push on the handle on the left side of the carriage (the place where the paper goes), the paper would rotate a small amount around the cylinder, advancing the document one line. If you had finished typing one line, and wanted to continue on to the next, you pushed harder, both advancing a line and sliding the carriage all the way to the right, then resuming typing left to right again as the carriage traveled with each keystroke. Needless to say, word-wrap was the default setting for all word processing of the era. P:D

其他回答

这些是非印刷字符,与“新行”的概念有关。\n是换行。\r是回车。在不同的平台上,相对于有效的新行,它们有不同的含义。在windows中,新行是\r\n。在linux中,\n。在mac中,\r。

实际上,将它们放在任何字符串中,都会对字符串的打印结果产生影响。

“\n”是换行字符。这意味着结束当前行,并为正在阅读它的任何人转到新的行。

\r是回车,光标向后移动,就像我要做-一样

printf("stackoverflow\rnine")
ninekoverflow

表示已将光标移到“stackoverflow”的开头,并覆盖开始的4个字符,因为“nine”有4个字符长。

\n是新行字符,它改变行并将光标移到新行开头,如-

printf("stackoverflow\nnine")
stackoverflow
nine

\f是进给,它的用途已经过时了,但它被用于缩进

printf("stackoverflow\fnine")
stackoverflow
             nine

如果我这样写

printf("stackoverflow\fnine\fgreat")
stackoverflow
             nine
                 great

\f用于换页。 在控制台中看不到任何效果。但是当你在文件中使用这个字符常量时,你就能看到区别了。

另一个例子是,如果你可以将输出重定向到一个文件,那么你就不必写文件或使用文件处理。

为例:

用c++编写以下代码

void main()    
{
    clrscr();
    cout<<"helloooooo" ;

    cout<<"\f";
    cout<<"hiiiii" ;

}

当你编译它的时候,它会生成一个exe(for exe . abc.exe)

然后你可以使用这个重定向输出到一个文件:

ABC > xyz.doc

然后打开xyz.doc文件,您可以看到helloo和hiiii....之间的实际分页符

Carriage return and line feed are also references to typewriters, in that the with a small push on the handle on the left side of the carriage (the place where the paper goes), the paper would rotate a small amount around the cylinder, advancing the document one line. If you had finished typing one line, and wanted to continue on to the next, you pushed harder, both advancing a line and sliding the carriage all the way to the right, then resuming typing left to right again as the carriage traveled with each keystroke. Needless to say, word-wrap was the default setting for all word processing of the era. P:D