Carriage Return, Line Feed and New Line
Asked Answered
D

1

34

What are the differences among Carriage Return, Line Feed and New line? Does it depend on OS? Why do we need to use all of them just for getting to next line?

Deth answered 12/9, 2012 at 11:8 Comment(0)
T
42

Generally, a "new line" refers to any set of characters that is commonly interpreted as signaling a new line, which can include:

  • CR LF on DOS/Windows
  • CR on older Macs
  • LF on Unix variants, including modern Macs

CR is the Carriage Return ASCII character (Code 0x0D), usually represented as \r. LF is the Line Feed character (Code 0x0A), usually represented as \n.

Original typewriter-based computers needed both of these characters, which do exactly what they say: CR returned the carriage to the left side of the paper, LF fed it through by one line. Windows kept this sequence unmodified, while Unix variants opted for more efficient character usage once they were only needed symbolically.

Make sure you look for a platform-agnostic new line symbol or function if you need to represent this sequence in code. If not, at least make sure that you account for the above three variants.

More on the history: The Great Newline Schism - Coding Horror

Tailback answered 12/9, 2012 at 11:19 Comment(2)
It's worth noting that the "n" in the C escape code \n is an abbreviation for "newline", and so this line feed character is sometimes referred to simply as "the newline character".Grotto
\n is NOT the same as LF - it has the special meaning of representing the OS-specific symbols used for New-line. Unix has chosen to take the "go one line down" to be the same as "go one line down and to the start of the line", other systems have chosen differently. And even more importantly - the source does not need to be ASCII to begin with - so no LF there at all.Frimaire

© 2022 - 2024 — McMap. All rights reserved.