以下控制字符的含义:

回车 换行 换页


当前回答

在老式的纸质打印机终端上,前进到下一行需要两个动作:将打印头移回水平扫描范围的起点(回车)和前进正在打印的纸卷(换行)。

由于我们不再使用纸质打印机终端,这些动作已经无关紧要了,但用来发出信号的字符以各种形式存在。

其他回答

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

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

Consider an IBM 1403 impact printer. CR moved the print head to the start of the line, but did NOT advance the paper. This allowed for "overprinting", placing multiple lines of output on one line. Things like underlining were achieved this way, as was BOLD print. LF advanced the paper one line. If there was no CR, the next line would print as a staggered-step because LF didn't move the print head. FF advanced the paper to the next page. It typically also moved the print head to the start of the first line on the new page, but you might need CR for that. To be sure, most programmers coded CRFF instead of CRLF at the end of the last line on a page because an extra CR created by FF wouldn't matter.

when I was an apprentice in the Royal Signals many (50) years ago, teletypes and typewriters had "Carriage" with the printing head on them. When you pressed RETURN the Carriage would fly to the left. Hence Carriage Return (CR). You could just return the Carriage, but on mechanical typewriters, you'd use the Lever (much like a tremolo lever on an electric guitar) which would also do the Line Feed. Your next question is why would you not want the line feed? heh heh well in those days to delete characters we'd do a CR then use a Tip-ex-like paper in between the hammerheads and paper and type the same keys to over-write with white ink. Some fancy typewriters had a key you could press. So there you go.

在老式的纸质打印机终端上,前进到下一行需要两个动作:将打印头移回水平扫描范围的起点(回车)和前进正在打印的纸卷(换行)。

由于我们不再使用纸质打印机终端,这些动作已经无关紧要了,但用来发出信号的字符以各种形式存在。

Apart from above information, there is still an interesting history of LF (\n) and CR (\r). [Original author : 阮一峰 Source : http://www.ruanyifeng.com/blog/2006/04/post_213.html] Before computer came out, there was a type of teleprinter called Teletype Model 33. It can print 10 characters each second. But there is one problem with this, after finishing printing each line, it will take 0.2 second to move to next line, which is time of printing 2 characters. If a new characters is transferred during this 0.2 second, then this new character will be lost.

于是科学家们找到了解决这个问题的方法,他们在每行后面加了两个结尾字符,一个是“回车”,这是告诉打印机将打印头移到左边。另一个是“换行”,它告诉打印机将纸张上移一行。

后来,计算机变得流行起来,这两个概念都用在了计算机上。当时的存储设备很贵,所以有科学家说每行末尾加两个字很贵,一个就够了,所以用哪个就有一些争论。

在UNIX/Mac和Linux中,'\n'被放在每行的末尾,在Windows中,'\r\n'被放在每行的末尾。这种使用的结果是,如果在Windows中打开,UNIX/Mac中的文件将显示在一行中。而Windows中的文件如果在UNIX或Mac中打开,则每行末尾会有一个^M。