What are carriage return, linefeed, and form feed?
Asked Answered
S

13

668

What is the meaning of the following control characters:

  1. Carriage return

  2. Line feed

  3. Form feed

Saturated answered 22/6, 2010 at 8:40 Comment(5)
What does "carriage return" mean? I don't see any "carriage" "returning" on my laptop. :sEinsteinium
@JeanHominal the carriage basically means the cursor (or the pointer in ancient typewriters), and return means to return to the beginning of that line.Commandment
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.Kyser
I'm surprised these were the only three control characters you wondered about.Frondescence
Canonical candidate (2009): Difference between CR LF, LF and CR line break types?Firm
C
791

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.

Conclusion answered 23/6, 2010 at 1:28 Comment(6)
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.Badly
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.Smuggle
Isn't that feed control \x0c?Durrace
"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.Loach
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.Hengel
Also... * carriage return = Ctrl-M or ^M * line feed = Ctrl-J or ^J * form feed = Ctrl-L or ^L See here.Interesting
O
181

\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
Other answered 13/2, 2016 at 1:50 Comment(5)
But this example doesn't work in high level languages !Apparel
@ᔕ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.Scrannel
How to pass all lines as one single line in a file?Salespeople
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.Grammatical
printf("123\f456\f789"); shows /><bold>123456789</bold> on console.Kozloski
M
64

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

Mcclenon answered 15/1, 2020 at 17:24 Comment(5)
Succinct answer like this should be encouraged.Lechery
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.Corpsman
@Lechery yes, although presenting more detail after the explanation can also be useful, especially in a case like this where there are other details to considerOutstay
Succinct answers are good for when you're only mildly interested and in a hurryLiggitt
Succinct answers perhaps hide traps. Especially in programming domain.Tuberosity
F
30

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.

Frayda answered 22/6, 2010 at 8:42 Comment(0)
A
28

\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....

Americaamerican answered 20/5, 2013 at 6:19 Comment(0)
B
24

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.

Bronchi answered 22/6, 2010 at 8:42 Comment(3)
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/TypewriterDeplete
@MarkLakata True for typewriters, but this answer is about old printing terminals. For example, the DEC LA36 (columbia.edu/cu/computinghistory/la36.html).Applicant
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.Isooctane
A
23

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.

Admonitory answered 9/7, 2015 at 9:52 Comment(4)
With the release of OSX (based on BSD unix), the Mac switched to '\n' - Unix line endings.Kittiekittiwake
"Files in UNIX/Mac will be displayed in one line if opened in Windows." WRITE.EXE (aka Wordpad) can display them just fine.Izanagi
CR and LF separated some 50 years before Teletype Model 33: see citeseerx.ist.psu.edu/viewdoc/…Reube
@ArtyomSkrobov Since the link is not working for me anymore, here's a new one: The Evolution of Character Codes, 1874-1968.Variegation
G
18

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.

Giffin answered 5/2, 2014 at 4:58 Comment(2)
This is the only answer that explains the full meaning of carriage return.Munday
@Munday agreed. The historical reasons for why these control characters exist is just as important as what they do.Matthieu
A
12

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.

Anguished answered 18/10, 2016 at 8:12 Comment(0)
L
4

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

Lexicon answered 22/6, 2010 at 8:42 Comment(0)
T
3

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

Ticktock answered 18/1, 2014 at 21:11 Comment(0)
I
2

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.

Isoprene answered 22/6, 2010 at 8:46 Comment(0)
I
2

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.

Indecency answered 12/10, 2021 at 18:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.