Information About

Newline




In Computing , a newline (also known as a '''line break''' or '''end-of-line''' / '''EOL''' character) is a special Character or sequence of characters signifying the end of a line of text. The name comes from the fact that the next character after the newline will appear on a ''new line'' — that is, on the next line below the text immediately preceding the newline. The actual codes representing a newline vary across hardware platforms and operating systems, which can be a problem when exchanging data between systems with different representations.

There is also some confusion as to whether newlines terminate or separate lines. If a newline is considered a separator, there will be no newline after the last line of a file. The general convention on most systems is to add a newline even after the last line, i.e. to treat newline as a line terminator. Some programs have problems processing the last line of a file if it isn't newline terminated. Conversely, programs that expect newline to be used as a separator will interpret a final newline as starting a new (empty) line. This can result in a different line count being reported for the file, but is otherwise generally harmless.


REPRESENTATIONS

Software applications and Operating System s usually represent a newline with one or two Control Characters :

  • Systems based on ASCII or a compatible character set use either LF (Line feed, 0A H ) or CR ( Carriage Return , 0Dh) individually, or CR followed by LF (CR+LF, 0D0Ah); see below for the historical reason for the CR+LF convention. These characters are based on printer commands: The ''line feed'' indicated that one line should feed out of the printer, and a ''carriage return'' indicated that the printer carriage should return to the beginning of the current line.

  • --- LF:    Multics , Unix and Unix-like systems ( GNU / Linux , AIX , Xenix , Mac OS X , etc.), BeOS , Amiga , RISC OS , and others

  • --- CR+LF: DEC RT-11 and most other early non-Unix non-IBM OSes, CP/M , MP/M , MS-DOS , OS/2 , Microsoft Windows

  • --- CR:    Commodore machines, Apple II Family and Mac OS up to Version 9

  • EBCDIC systems—mainly IBM mainframe systems, including Z/OS ( OS/390 ), I5/OS ( OS/400 )—use NEL (Next Line, 15h) as the newline character. Note that EBCDIC also has control characters called CR and LF, but the numerical value of LF differs from the one used by ASCII. Additionally, there are some EBCDIC variants that also use NEL but assign a different numeric code to the character.

  • CDC NOS used at least 4 six-bit characters with the character code 00 at the end of a 60-bit word to indicate end-of-line. Character code 00 was also used to represent the Colon character; thus if you entered too many consecutive ":" characters with the CDC text editor, you might unintentionally create an extra end-of-line.

  • OpenVMS uses a Record-based File System which stores text files as one record per line. In most file formats, no line terminators are actually stored, but the Record Management Services facility can transparently add a terminator to each line when it is retrieved by an application.

  • ''Fixed line length'' was used by some early Mainframe operating systems. In such a system, an implicit end-of-line was assumed every, say, 80 characters. No newline character was stored. If a file was imported from the outside world, lines shorter than the line length had to be padded with spaces, while lines longer than the line length had to be truncated. This mimicked the use of Punched Card s, on which each line was stored on a separate card, usually with 80 columns on each card.


Most textual Internet Protocols (including HTTP , SMTP , FTP , IRC and many others) mandate the use of ASCII CR+LF (0Dh 0Ah) on the protocol level, but recommend that tolerant applications recognize lone LF as well. In practice, there are many applications that erroneously use the C newline character '
'
instead (see section Newline In Programming Languages below). This leads to problems when trying to communicate with systems adhering to a stricter interpretation of the standards; one such system is the Qmail MTA that actively refuses to accept messages from systems that send bare LF instead of the required CR+LF.


UNICODE

The Unicode standard addresses the problem by defining a large number of characters that conforming applications should recognize as line terminators:

 LF:    Line Feed, U+000A

 CR:    Carriage Return , U+000D

 CR+LF: CR followed by LF, U+000D followed by U+000A

 NEL:   Next Line, U+0085

 FF:    Form Feed, U+000C

 LS:    Line Separator, U+2028

 PS:    Paragraph Separator, U+2029

This may seem overly complicated compared to an approach such as converting all line terminators to a single character, for example LF. The simple approach breaks down, however, when trying to convert a text file from an encoding like EBCDIC to Unicode and back. When converting to Unicode, NEL would have to be replaced by LF, but when converting back it would be impossible to decide if a LF should be mapped to an EBCDIC LF or NEL. The approach taken in the Unicode standard allows this transformation to be information-preserving while still enabling applications to recognize all possible types of line terminators.


HISTORY

ASCII was developed simultaneously by the ISO and the ASA, the predecessor organization to ANSI . During the period of 19631968 , the ISO draft standards supported the use of either CR+LF or LF alone as a newline, while the ASA drafts supported only CR+LF. The Multics operating system began development in 1964 and used LF alone as its newline. Unix followed the Multics practice, and later systems followed Unix.

The sequence CR+LF was in common use on many early computer systems that had adapted Teletype machines, typically an ASR33 , as a console device, because this sequence was required to position those printers at the start of a new line. On these systems text was often routinely composed to be compatible with these printers, since the concept of Device Driver s hiding such hardware details from the application was not yet well developed; applications had to talk directly to the teletype machine and follow its conventions. The separation of the two functions concealed the fact that the print head could not return from the far right to the beginning of the next line in one-character time. That is why the sequence was always sent with the CR first. In fact, it was often necessary to send extra characters (extraneous CRs or NULs, which are ignored) to give the print head time to move to the left margin. Even after teletypes were replaced by Computer Terminal s with higher Baud rates, many operating systems still supported automatic sending of these fill characters, for compatibility with cheaper terminals which required multiple character times to scroll the display.

MS-DOS , built upon a CP/M clone called 86-DOS (which Microsoft purchased and renamed), adopted CP/M's CR+LF; CP/M's use of CR+LF made sense for using computer terminals via serial lines. This convention was inherited by Microsoft's later Windows operating system.


NEWLINE IN PROGRAMMING LANGUAGES

To facilitate the creation of Portable programs, programming languages provide some abstractions to deal with the different types of newline sequences used in different environments.

The C Programming Language provides the Escape Sequence s '
'
(newline) and ' ' (carriage return). However, contrary to popular belief, these are in fact not required to be equivalent to the ASCII LF and CR control characters. The C standard only guarantees two things:
# Each of these escape sequences maps to a unique implementation-defined number that can be stored in a single char value.
# When writing a file in ''text mode'', '
'
is transparently translated to the native newline sequence used by the system, which may be longer than one character. (Note that a C implementation is allowed not to store newline characters in files. For example, the lines of a text file could be stored as rows of a SQL table or as fixed-length records.) When reading in text mode, the native newline sequence is translated back to '
'
. In ''binary mode'', the second mode of I/O supported by the C library, no translation is performed, and the internal representation of any escape sequence is output directly.

On Unix platforms, where C originated, the native newline sequence is ASCII LF (0x0A), so '
'
was simply defined to be that value. With the internal and external representation being identical, the translation performed in text mode effectively turns into a No-op , making text mode and binary mode behave the same. This has caused many programmers who developed their software on Unix systems to simply ignore the distinction completely, resulting in code that is not portable to different platforms.

Another common problem is the use of '
'
when communicating using an Internet protocol that mandates the use of ASCII CR+LF for ending lines. Writing '
'
to a text mode stream works correctly on Windows systems, but produces only LF on Unix, and something completely different on more exotic systems. Using "
"
in binary mode is slightly better, as it works on many ASCII-compatible systems, but still fails in the general case. One approach is to use binary mode and specify the numeric values of the control sequence directly, "
"
.

provide the same interpretation of '
'
as C. C++ also provides std::endl, which is a function that emits the underlying system's representation of a newline, then flushes the stream.

Java also provides '
'
and ' ' escape sequences. In contrast to C, these are guaranteed to represent the values 0x0A and 0x0D, respectively. The Java I/O libraries do not transparently translate these into platform dependent newline sequences on input or output. Instead, they provide functions for writing a full line that automatically add the native newline sequence, and functions for reading lines that accept any of CR, LF, or CR+LF as a line terminator (see BufferedReader.readLine() ).


COMMON PROBLEMS

The different newline conventions often cause text files that have been transferred between systems of different types to be displayed incorrectly. For example, files originating on Unix or Apple Macintosh systems may appear as a single long line on a Windows system. Conversely, when viewing a file from a Windows computer on a Unix system, the extra CR may be displayed as ^M at the end of each line or as a second line break.

The problem can be hard to spot if some programs handle the foreign newlines properly while others don't. For example, a Compiler may fail with obscure syntax errors even though the source file looks correct when displayed on the Console or in an Editor . Modern text editors generally recognize all flavours of CR / LF newlines and allow the user to convert between the different standards. Web Browser s are usually also capable of displaying text files of different types.

The s (for example, inspection of Filename Extension s) to automatically select either binary or ASCII mode, but in the end it is up to the user to make sure his or her files are transferred in the correct mode. If there is any doubt as to the correct mode, binary mode should be used, as then no files will be altered by FTP, though they may display incorrectly.


CONVERSION UTILITIES

Generally, using a text editor is the simplest and most convenient way of converting a text file between different newline formats; most modern editors can read and write files using at least the different ASCII CR/LF conventions. Unfortunately, the standard Windows editor Notepad is not one of them (though Wordpad is).

On Windows systems without a better editor, the old MS-DOS editor EDIT that still ships with modern Windows versions can be used to convert a Unix text file to DOS/Windows newlines. A simple way of doing this is by creating a shortcut to EDIT on the desktop (context menu / New / Shortcut / "edit" / Next / Finish), dragging the text file in question onto it, and then saving the file again (File / Save).

Editors are often unsuitable for converting larger files. For larger files (on Windows NT/2000/XP) you can use the following command: