1

Yesterday I added an answer to How to add a line break to text in UI5?. While trying to specify the question with more tags, I realized that there are two similar tags available on Stack Overflow: and .

The differentiation between LF and CR is pretty clear. But what is the difference between the terms "newline" and "line break"? Aren't they synonym to each other?

The excerpt here on Stack Overflow says:

Newline refers to [...] a line break.

... while the disambiguation page of "Line break" from Wikipedia says:

Line break may refer to [...] newline.

And from that "Newline" page:

To denote a single line break, Unix programs use line feed [...] while most programs common to [...] Windows use carriage return+line feed.

Are there any technical standards or guidelines that make a clear distinction between those two terms in the software industry? Or can they be used interchangeably, having no technical difference?

My current assumption is that there is no difference: "line break" describes the result from either soft return (⇧ Shift+↵ Enter) or hard return (↵ Enter), whereas "newline" is a technical term for "line break" but has the same result.

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170

1 Answers1

3

I interpret line break as a semantic meaning. What you tell a typographer when you see a \n. C language "new line" means a line break. This is like A (0x41) in ASCII is the upper case of Latin letter a. So a name, a code, and a meaning.

It is like "SP" (\u0020) is a space character. There are many other space characters: C recognizes also TAB as space character. HTML recognize new line as space character (and not as a line break (but in <pre>).

CR and LF were defined by ASCII, and used together because typewritter usually worked in such manner, and to give some more time to move (they were connected serially, so with a timed inputs). You may find the scanned document of old ASCII about the meaning of control characters, but that changed a lot. \0 is now a end of string, before it was just a filling character (or to quit a sequence). Apple used CR, Unix LF as what typographers used as line break.

If you want a list of names and aliases, Unicode provides such names (but as usually, there were errors, so BELL is now used for a emoji, and so the old alias of BEL is not more valid, for control code). There are also several sources about standardization of control codes (mainly used for escape sequances, and so). But also this is not fully follower. Terminals tend to have quirks.

Giacomo Catenazzi
  • 8,519
  • 2
  • 24
  • 32
  • So on windows, do you have to deal with both `\r\n` to end lines, or is that simply `\n` in C? – user129393192 Jul 24 '23 at 08:12
  • It depends. On Unix it is simple. On Windows you can either handle single ASCII characters (so `\r` and `\n`) so raw files (which we call *binary*, because usually we use them for binary), or as text, where libraries and or language will handle them. Python allow all common cases independent to operating system (so you decode in the same way in Unix a file created in Windows). C (and related programs) check operating system way, and they give you (when you read) only one character, but when on the disk side (reading/writing) the two byte version is used. – Giacomo Catenazzi Jul 24 '23 at 08:19
  • So, it depends on how you are handling the program. You may use both in the same program. E.g. one for logs and display things, and the other e.g. to read strings in a binary file (like descriptions or tags or other metadata inside a picture). Or when sending things in internet or to a serial port, you may need to use one or the other, depending on protocol. Normally in C, just use `\n` and let library to do the conversions – Giacomo Catenazzi Jul 24 '23 at 08:21
  • You mean `stdio` right? If you use POSIX `read` and `write`, then I imagine you have to deal with it if you want to port to a system where the convention is not to use a `LF` character as the line-delimiter. – user129393192 Jul 24 '23 at 19:18
  • I avoid to use `stdio` here, because it is specific to C (and used by other language libraries, but not all). Your question has not specific language tag. Other languages may not use `stdio` (or in general `libc`), and as I said Python uses *universal new lines*, so a different semantic: python can read dos, unix, old-mac text files on all systems. libc may just convert only native (to platform) new lines. – Giacomo Catenazzi Jul 25 '23 at 05:47