What are carriage return, linefeed, and form feed?

后端 未结 12 1432
我寻月下人不归
我寻月下人不归 2020-11-22 14:26

What is the meaning of the following control characters:

  1. Carriage return

  2. Line feed

  3. Form feed

相关标签:
12条回答
  • 2020-11-22 15:15

    "\n" is the linefeed character. It means end the present line and go to a new line for anyone who is reading it.

    0 讨论(0)
  • 2020-11-22 15:17

    Those are non-printing characters, relating to the concept of "new line". \n is linefeed. \r is carriage return. On different platforms they have different meanings, relative to a valid new line. In windows, a new line is \r\n. In linux, \n. In mac, \r.

    In practice, you put them in any string, and it will have effect on the print-out of the string.

    0 讨论(0)
  • 2020-11-22 15:18

    As a supplement,

    1, Carriage return: It's a printer terminology meaning changing the print location to the beginning of current line. In computer world, it means return to the beginning of current line in most cases but stands for new line rarely.

    2, Line feed: It's a printer terminology meaning advancing the paper one line. So Carriage return and Line feed are used together to start to print at the beginning of a new line. In computer world, it generally has the same meaning as newline.

    3, Form feed: It's a printer terminology, I like the explanation in this thread.

    If you were programming for a 1980s-style printer, it would eject the paper and start a new page. You are virtually certain to never need it.

    http://en.wikipedia.org/wiki/Form_feed

    It's almost obsolete and you can refer to Escape sequence \f - form feed - what exactly is it? for detailed explanation.

    Note, we can use CR or LF or CRLF to stand for newline in some platforms but newline can't be stood by them in some other platforms. Refer to wiki Newline for details.

    LF: Multics, Unix and Unix-like systems (Linux, OS X, FreeBSD, AIX, Xenix, etc.), BeOS, Amiga, RISC OS, and others

    CR: Commodore 8-bit machines, Acorn BBC, ZX Spectrum, TRS-80, Apple II family, Oberon, the classic Mac OS up to version 9, MIT Lisp Machine and OS-9

    RS: QNX pre-POSIX implementation

    0x9B: Atari 8-bit machines using ATASCII variant of ASCII (155 in decimal)

    CR+LF: Microsoft Windows, DOS (MS-DOS, PC DOS, etc.), DEC TOPS-10, RT-11, CP/M, MP/M, Atari TOS, OS/2, Symbian OS, Palm OS, Amstrad CPC, and most other early non-Unix and non-IBM OSes

    LF+CR: Acorn BBC and RISC OS spooled text output.

    0 讨论(0)
  • 2020-11-22 15:19

    On old paper-printer terminals, advancing to the next line involved two actions: moving the print head back to the beginning of the horizontal scan range (carriage return) and advancing the roll of paper being printed on (line feed).

    Since we no longer use paper-printer terminals, those actions aren't really relevant anymore, but the characters used to signal them have stuck around in various incarnations.

    0 讨论(0)
  • 2020-11-22 15:22

    In Short :

    Carriage_return(\r or 0xD): To take control at starting of same line.

    Line_Feed(\n or 0xA): To Take control at starting of next line.

    form_feed(\f or 0xC): To take control at starting of next page.

    0 讨论(0)
  • 2020-11-22 15:24

    Carriage return means to return to the beginning of the current line without advancing downward. The name comes from a printer's carriage, as monitors were rare when the name was coined. This is commonly escaped as \r, abbreviated CR, and has ASCII value 13 or 0x0D.

    Linefeed means to advance downward to the next line; however, it has been repurposed and renamed. Used as "newline", it terminates lines (commonly confused with separating lines). This is commonly escaped as \n, abbreviated LF or NL, and has ASCII value 10 or 0x0A. CRLF (but not CRNL) is used for the pair \r\n.

    Form feed means advance downward to the next "page". It was commonly used as page separators, but now is also used as section separators. (It's uncommonly used in source code to divide logically independent functions or groups of functions.) Text editors can use this character when you "insert a page break". This is commonly escaped as \f, abbreviated FF, and has ASCII value 12 or 0x0C.


    As control characters, they may be interpreted in various ways.

    The most common difference (and probably the only one worth worrying about) is lines end with CRLF on Windows, NL on Unix-likes, and CR on older Macs (the situation has changed with OS X to be like Unix). Note the shift in meaning from LF to NL, for the exact same character, gives the differences between Windows and Unix. (Windows is, of course, newer than Unix, so it didn't adopt this semantic shift. I don't know the history of Macs using CR.) Many text editors can read files in any of these three formats and convert between them, but not all utilities can.

    Form feed is a bit more interesting (even though less commonly used directly), and with the usual definition of page separator, it can only come between lines (e.g. after the newline sequence of NL, CRLF, or CR) or at the start or end of the file.

    0 讨论(0)
提交回复
热议问题