What is a carriage return?

I am trying to figure out the difference between print and println.

It says one has a carriage return. What is that?

It is a control character, which means it is not being printed but but tells the “printer”(in modern days it is the screen) to do something. In CR (carriage return) case it tells the cursor ( the position at which the next character will appear) to go to the beginning of the line.

If you want to the cursor to go to the beginning of the line and go one line below it (which println does) you need to combine it with LF (line feed) control character.

For example if your string looks like this “abc\r\ndef”, it will be printed like this:

abc
def

\r is the character for carriage return.
\n is the character for line feed.
On windows, ‘\n’ combines both.

alkhimey:
It is a control character, which means it is not being printed but but tells the "printer"(in modern days it is the screen) to do something. In CR (carriage return) case it tells the cursor ( the position at which the next character will appear) to go to the beginning of the line.

If you want to the cursor to go to the beginning of the line and go one line below it (which println does) you need to combine it with LN (line feed) control character.

For example if your string looks like this "abc\r\ndef", it will be printed like this:

abc

def




\r is the character for carriage return.
\n is the character for line feed.
On windows, '\n' combines both.

Minor details
"CR" is a single character - carriage return - ASCII name of the non printing / control character
"\n" is a coded parameter "n" send to the "printf" ( basic C ) or other printing functions.

Back in the ancient times before we had word processors people had to use these things called typewriters. When using a type writer, as you press the keys and the letters appeared on the paper the carriage would move one space to the left and get ready for the next character. When you got to the end of the line you would have to grab this big metal bar and pull it to make the carriage return all the way to the left and it would also advance the paper one line up so that the next line of typing would be below the last one.

Later, right before computers hit it big, there were electric typewriters where instead of grabbing that metal bar and doing it yourself you had a button called "Carriage Return" that would do the same thing. The ascii symbol \cr is a holdover from those days. It doesn't make much sense nowadays, but when computers and ascii were new it was something that you often had to tell the printer or it would just write right off the right side of the page.

Nowadays we use it and a '\n' newline character to indicate the end of a line of text.

In terms of print and println, it means this...

Serial.print("Hello");
Serial.print("World");
Serial.print("From");
Serial.print("Me");

which prints

HelloWorldFromMe

As opposed to :

Serial.println("Hello");
Serial.println("World");
Serial.println("From");
Serial.println("Me");

which prints

Hello
World
From
Me

Thanks for the help!

That clears it up for me. Especially the reference to typewriters. It make sense now.

In terms of the specific difference between print() and println()
println() sends a carriage return character \r (hex 0x0d) and
a linefeed (newline) character \n (hex 0x0a)
after sending the other characters.

On windows, '\n' combines both.

Actually windows uses the combination of \r\n (carriage return and linefeed)
to represent a line ending in text files
while unix type systems have always used a single \n to represent a line ending.

--- bill

bperrybap:
while unix type systems have always used a single \n to represent a line ending.

In C code, yes '\n' denotes both the historical carriage return and line feed, but the terminal driver tends to have the onlcr stty option set, which is converted by the OS into sending '\r\n' to the devices. Of course these days, a lot of people don't use command line processing anymore and don't use real or virtual terminals (like xterm), so for them it is a moot point.

"CR" is a single character

"CR" is a string containing the two characters C and R followed by the NULL terminator

"\n" is a coded parameter "n" send to the "printf"

The backslash inside a string is an escape character and the character following it is interpreted differently.
\n is translated (by the compiler) to the linefeed character
\r is translated to the carriage return character
\t is translated to the horizontal tab character.
\ is translated to a single backslash character
These and other escaped characters are valid in any C string, not just in printf strings.
So, "\n" is a string consisting of a linefeed character followed by the NULL terminator.

The special character in a printf string is %.

Pete