stringspecial-charactersline-endings

What's the difference between \n and \r\n?


They both mean "new line" but when is one used over the other?


Solution

  • \r\n is a Windows Style

    \n is a POSIX Style

    \r is a old pre-OS X Macs Style, Modern Mac's using POSIX Style.


    \r is a carriage return and \n is a line feed. On old computers without monitors, you needed to use paper and a printer to get a program's result to the user. In order for new lines in your printed file to start at the leftmost column, files needed both a \n for line Feed, and a \r for get carriage return to the most left position. This is a carryover from the computers of yesteryear, and is generally now only seen on Windows.