ChatGPT解决这个技术问题 Extra ChatGPT

What are carriage return, linefeed, and form feed?

What is the meaning of the following control characters:

Carriage return Line feed Form feed

What does "carriage return" mean? I don't see any "carriage" "returning" on my laptop. :s
@JeanHominal the carriage basically means the cursor (or the pointer in ancient typewriters), and return means to return to the beginning of that line.
I do not see any wires terminated (or any glass panes for that matter) in my "terminal" "window", nor is there a "teletype" attached to /dev/tty1. They've all become metaphors.
I'm surprised these were the only three control characters you wondered about.

R
Roger Pate

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 0xD.

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 0xA. 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. 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 0xC.

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

The most important interpretation is how these characters delimit lines. Lines end with NL on Unix (including OS X), CRLF on Windows, and CR on older Macs. Note the shift in meaning from LF to NL, for the exact same character, gives the differences between Windows and Unix, which is also why many Windows programs use CRLF to separate instead of terminate lines. Many text editors can read files in any of these three formats and convert between them, but not all utilities can.

Form feed is much less commonly used. As page separator, it can only come between lines or at the start or end of the file.


I can confirm that OS X uses NL as a line terminator, just like Unix (because OS X is a Unix). CR was used in Mac OS 9 and below.
The FF char may seem strange today (along with the "CTRL-G" bell char) but it had more relevance back when video displays were far less common and hard copy terminals were used as data displays.
Isn't that feed control \x0c?
"I don't know the history of Macs using CR." That probably came from the Apple II using CR. CR was common on other 8-bit systems, too, like the Commodore and Tandy. ASCII wasn't universal on these systems: Commodore used PETSCII, which had LF at 0x8d (!). Atari had no LF character at all. For whatever reason, CR = 0x0d was more-or-less standard.
It might be worth noting that some programs actually do consider line-ending characters as separators instead of terminators, although the only one I can think of off the top of my head is Ace editor, which will let you save a file without a trailing line ending. On the other hand, Nano editor will always append a line ending, and a Bash script won't read a line at the end of a file if it doesn't have a trailing newline.
e
e0k

\r is carriage return and moves the cursor back like if i will do-

printf("stackoverflow\rnine")
ninekoverflow

means it has shifted the cursor to the beginning of "stackoverflow" and overwrites the starting four characters since "nine" is four character long.

\n is new line character which changes the line and takes the cursor to the beginning of a new line like-

printf("stackoverflow\nnine")
stackoverflow
nine

\f is form feed, its use has become obsolete but it is used for giving indentation like

printf("stackoverflow\fnine")
stackoverflow
             nine

if i will write like-

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

But this example doesn't work in high level languages !
@ᔕIᑎᗩKᗩᖇᐯᗩᑎᗪI it does: you can at least do the same in Linux shell using printf utility from coreutils. Or even using echo -en which is a bash builtin.
How to pass all lines as one single line in a file?
Form feed is not used for indentation. If you send few of those to a printer it'll spit out paper. What you've described as \f is actually \v - a vertical tab, granted both look the same in the console.
printf("123\f456\f789"); shows /><bold>123456789</bold> on console.
i
informatik01

In short:

Carriage return (\r or 0xD): To take control at starting on the same line.

Line feed (\n or 0xA): To Take control at starting on the next line.

Form feed (\f or 0xC): To take control at starting on the next page.

More details and more control characters can be found on the following page: Control character


Succinct answer like this should be encouraged.
Succint answers are elegant only on few domains such as in mathematics. Windows uses both 0x0A 0x0D, and these are choices that were arbitrarily made. The concepts does not stand-alone, therefore succint answers will not fit in these situations.
@user5280911 yes, although presenting more detail after the explanation can also be useful, especially in a case like this where there are other details to consider
Succinct answers are good for when you're only mildly interested and in a hurry
t
tanascius

Have a look at Wikipedia:

Systems based on ASCII or a compatible character set use either LF (Line feed, '\n', 0x0A, 10 in decimal) or CR (Carriage return, '\r', 0x0D, 13 in decimal) individually, or CR followed by LF (CR+LF, 0x0D 0x0A). These characters are based on printer commands: The line feed indicated that one line of paper should feed out of the printer, and a carriage return indicated that the printer carriage should return to the beginning of the current line.


K
Kent Pawar

\f is used for page break. You cannot see any effect in the console. But when you use this character constant in your file then you can see the difference.

Other example is that if you can redirect your output to a file then you don't have to write a file or use file handling.

For ex:

Write this code in c++

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

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

}

and when you compile this it generate an exe(for ex. abc.exe)

then you can redirect your output to a file using this:

abc > xyz.doc

then open the file xyz.doc you can see the actual page break between hellooo and hiiii....


A
Amber

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.


Actually 'carriage return' does not move the print head to the left, but moves the carriage which holds the platen to the right. There used to be a long lever that was actuated by the left hand to do this. See en.wikipedia.org/wiki/Typewriter
@MarkLakata True for typewriters, but this answer is about old printing terminals. For example, the DEC LA36 (columbia.edu/cu/computinghistory/la36.html).
The carriage return on old mechanical and some electric typewriters did indeed move the carriage and platen to the right. Since the document was held in the carriage moving the carriage to the right, had the effect of moving the print position to the left. On electric typewriters with moving print heads and most teletype machines, the carriage and platen did not move horizontally. Instead, the print head moved. Here the carriage return returns the print head to the left. In both cases, the carriage return returns the print position to the beginning of the line.
m
miken32

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.

So scientists found a way to solve this problem, they add two ending characters after each line, one is 'Carriage return', which is to tell the printer to bring the print head to the left.; the other one is 'Line feed', it tells the printer to move the paper up 1 line.

Later, computer became popular, these two concepts are used on computers. At that time, the storage device was very expensive, so some scientists said that it was expensive to add two characters at the end of each line, one is enough, so there are some arguments about which one to use.

In UNIX/Mac and Linux, '\n' is put at the end of each line, in Windows, '\r\n' is put at the end of each line. The consequence of this use is that files in UNIX/Mac will be displayed in one line if opened in Windows. While file in Windows will have one ^M at the end of each line if opened in UNIX or Mac.


With the release of OSX (based on BSD unix), the Mac switched to '\n' - Unix line endings.
"Files in UNIX/Mac will be displayed in one line if opened in Windows." WRITE.EXE (aka Wordpad) can display them just fine.
CR and LF separated some 50 years before Teletype Model 33: see citeseerx.ist.psu.edu/viewdoc/…
D
Dick Guertin

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.


This is the only answer that explains the full meaning of carriage return.
@Lee agreed. The historical reasons for why these control characters exist is just as important as what they do.
C
Community

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.


v
vpit3833

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


T
TheLastWordSword

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


P
Palantir

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.


N
Nayana Chandran

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.