What’s the difference between CR, LF, and CRLF line endings?

0
1
Asked By MellowCedar27 On

My Android text editor lets me save files using three line-ending formats: CR (classic Macintosh), LF (Linux and modern macOS), or CRLF (Windows). The same file is about 93.8 KB with CR or LF, but grows to roughly 101 KB with CRLF. When I open the versions in Windows Notepad, they look the same, and file comparison reports no meaningful content difference. What exactly do CR, LF, and CRLF represent, and which format should I use when editing the file across an Android phone and a Windows laptop? Is LF preferable because it saves space, or is CRLF more compatible?

3 Answers

Answered By VioletHarbor3 On

If the file will be processed by a particular program, use the format that program expects. Windows traditionally uses CRLF, whereas Unix-based systems and most current cross-platform tools use LF. Editors often recognize either style and may silently convert it when saving. There is no universal format guaranteed to satisfy every application, so LF is the practical cross-platform choice, with conversion to CRLF when an older Windows tool specifically needs it.

PlainRiver9 -

The fact that the file looks identical in Notepad only means that Notepad understands the chosen line endings; it doesn’t mean the underlying bytes are the same.

Answered By BrightOtter5 On

CR means carriage return and LF means line feed. They originated as separate commands for moving a printing mechanism: CR returns to the start of the line, while LF advances to the next line. Classic Mac systems used CR alone, Unix-like systems use LF, and Windows traditionally uses CRLF. CRLF takes an extra byte per line because it stores both characters. For most modern software, LF is a good default and is supported by current Windows editors as well.

NimbleQuartz8 -

There isn’t one line ending that works perfectly with every old program. Most modern tools handle both LF and CRLF, but if a particular application has trouble, convert the file to the format it expects.

Answered By CopperLynx42 On

Use LF unless you know that a specific older Windows program requires CRLF. LF works on Linux, modern macOS, Android, and usually Windows, while CR by itself is mainly relevant to obsolete classic Mac software. The size difference is expected: every line ending is one byte with LF, but two bytes with CRLF. It normally isn’t worth choosing a format solely to save space unless the file is extremely large.

SageMoth16 -

A file can appear to have different sizes on different devices even though the visible text is identical, because the line-ending bytes differ. A comparison tool that understands line endings may treat those versions as equivalent.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.