What is the difference between \n and \r\nline breaks?

Asked 2 years ago, Updated 2 years ago, 130 views

What is the difference between \n and \r\nline breaks?
\nThere is an environment where it doesn't work.

text-file character-code

2022-09-29 22:08

3 Answers

\n is the LF character (Line Feed) and \r is the control character called the carriage return.

There are three ways to express a new line of text, depending on the system:The representative OS using that expression method is also included.

  • \n (LF): Unix-based OS in general, Mac OS X
  • \r\n (CR+LF):Windows OS
  • \r (CR):Old Mac OS (9 and earlier)

Although it is natural to use \n single characters as expressions on your computer, there is a historical history of using \r\n originally derived from a typewriter. (CR moves the printer back to the left + LF scrolls a line of paper)


2022-09-29 22:08

Communication protocols tend to use CRLF (\r\n).For example, HTTP and mail protocols specify CRLF as the newline code.


2022-09-29 22:08

UNIX operating systems use \n and
Microsoft operating systems use \r\n.
\r\n is usually converted to \n, so there is no problem
For binary handling or
This can be a problem if you are trying to run UNIX-like OSes like Cygwin.


2022-09-29 22:08

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.