Pete Freitag Pete Freitag

What is the difference between ASCII Chr(10) and Chr(13)

Updated on December 07, 2023
By Pete Freitag
misc

typewriters have a carriage return

Writing this because I can never remember which ascii code is \n and which is \r. Usually I end up googling my ASCII Cheatsheet for the answer. So here it is fully explained in case you are curious:

What is Chr(10)

The ASCII character code 10 is sometimes written as \n and it is sometimes called a New Line or NL. ASCII character 10 is also called a Line Feed or LF.

On a UNIX based operating system such as Linux or Mac it is all you typically use to delineate a line in a file.

What is Chr(13)

The ASCII character code 13 is called a Carriage Return or CR. On windows based computers files are typically delimited with a Carriage Return Line Feed or CRLF. So that is a Chr(13) followed by a Chr(10) that compose a proper CRLF.

Things such as HTTP headers or MIME mail headers are also delimited with a CRLF.

If you have ever opened a file with notepad on windows and found that it appears that everything is on one line, but then open it in another editor to find that the lines appear, the reason for that is that the file is delimited with only LF Chr(10) and not CRLF. Notepad is not smart enough to know the difference and plops everything on one line. Most text editors on windows besides notepad have no problem with working on files that are only delimited with \n or Chr(10).

This all dates back to the typewriter

The term Carriage Return actually dates back to when typewriters were commonly used. When you hit enter on a typewriter it will advance the paper to the next line (line feed!), however unless you return the carriage (that lever that you move to the left) you would keep typing, here's an example:

I will hit enter now
                    but I forgot to return the carriage

So what should I use as a file line delimiter?

I tend to use only Chr(10) because it takes less space. Unless you have a use case where people will be opening the files you create with notepad, this should work fine.

Also if your file needs to be interfaced with a typewriter, I would make sure you are using a Carriage Return ;-)

Is it Char(10) or Chr(10)?

The name of the function to return an ascii character from an integer character code is not consistent across programming languages. So it depends what language you are using, here is a handy table with some popular languages:

LanguageLine Feed / New LineCarriage Return
Regular Expressions or RegEx\n\r
SQLCHAR(10)CHAR(13)
CFML / ColdFusionChr(10)Chr(13)
PHPChr(10)Chr(13)
PythonChr(10)Chr(13)
JavaScriptString.fromCharCode(10)String.fromCharCode(13)
JavaCharacter.toString(10)Character.toString(13)


ascii

What is the difference between ASCII Chr(10) and Chr(13) was first published on May 17, 2019.


Discuss / Follow me on Twitter ↯

Comments

When I was a kid we had this manual typewriter in the basement and there was no power or enter key. You just had a big lever and a little lever. The little lever could be set to 1 or 2 for single or double spacing. If you just pushed the big lever it a little, it would advance 1/2 the little lever setting. A little more it would advance the whole little lever setting, and you had to hold it while moving the carriage to the left to do the Carriage Return. If your hand slipped, it would make you do the line feed(s) again to get it back to the beginning of the line. It's amazing to think about how technology has change over the years.
by Steve on 04/09/2020 at 12:25:27 PM UTC
Dear Pete,
I think if you look up how an electro-mechanical Telex machine used to work you will find that you needed both carriage return AND line feed in that order for it to work nicely. It took longer for the print head to go all the way to the left so you initiated that first.
If you wanted to cross out a line, you gave carriage return and a load of XXXXX...
AND Yes, I'm old and ugly enough to have used a Telex machine!! :-)
With Regards From
Roger Jefferyes
by Roger Jefferyes on 07/13/2020 at 1:03:19 PM UTC
When you type on a manual typewriter, the carriage moves to the left so the characters are typed "to the right". So when returning the carriage, you are moving the carriage from the left to the right. Right?
by Rufus on 10/07/2020 at 9:45:30 PM UTC
When writing English on paper with a pen by hand, the paper is stationary and ones hand moves to the right across the paper. When the righthand edge of the paper is reached ones hand moves down to the left edge to write the next line.

On a manual mechanical typewriter, the paper is mounted on a roller called a carriage. Between the paper and the key hammers is an ink ribbon. When typing, the key action moves the carriage to the left after the character is impressed on the paper through the ink ribbon. To return to the writing by hand analogy, it would be like keeping the pen hand still and moving the paper underneath to the left instead.

On a manual mechanical typewriter the carriage has a carriage return lever. The first part of its travel advances the paper on the carriage roller. That is the line feed. Continued pressure on the carriage return lever causes the carriage to slide to the right until the left edge of the paper is under the character key hammers. That is a carriage return.

As printing technology developed, the print head moved left to right and paper stayed in same place just moving up a line. However the terminology remained true to a mechanical typewriter.
by Mark on 11/04/2022 at 4:46:11 PM UTC