6

What is the difference between print( ) and println( )?
I see none when I run a code.

  • 2
    Really? Try a few of each in sequence and see if you see any difference. – Wirewrap Apr 19 '15 at 11:35
  • Ya, now I see a difference – Phyman 100 Apr 19 '15 at 11:40
  • 1
    The answer to this question could have easily be found on the Arduino site. – Mathsman 100 Apr 19 '15 at 11:51
  • 1
    The ln from println comes from line, meaning it will print a new line character at the end – rslite Apr 19 '15 at 14:43
5

print() prints whatever you send in.
println() does the same thing, only after using println, anything new that is printed gets printed in next line, I.e. a new line is formed.

  • Could you post your code please, as my answer is kind of incomplete now. – Mathsman 100 Apr 19 '15 at 11:37
7

An easy way to see the difference is using Serial.print();/Serial.println();.

print();

will print out whatever you input wherever the cursor currently is. For example:

Serial.print("Test");
Serial.print("Words");

This will print:

TestWords_

The underscore marks where the cursor is (and therefore where the next print command will start). In contrast, the code:

Serial.println("Test");
Serial.println("Words");

will print the following:

Test
Words
_

You can also print multiple statements and then follow with println like so (note the space at the end/beginning of the strings):

Serial.print("These ");
Serial.print("Test");
Serial.println(" Words.");

to get the following output:

These Test Words.
_

You can also use println(); to add a newline character in general. If you would print a variable that doesn't return a newline character, println(); can be used for formatting. Example:

int x = 50;
Serial.print(x);
Serial.println();

This will print:

50
_

Finally, you can add in special characters like a tab \t inside your quotes for formatting. Example:

Serial.println("Test\tTest")

This will return:

Test    Test
_
  • 1
    Your answer tells us that println() function prints what's in the parenthesis then prints a newline character rather that printing a newline character first and then printing what's inside the parenthesis. Your idea of underscore for showing the cursor position is quite very nice. +1 for all that. – Devesh Saini Jun 27 '16 at 11:19
1

The print("aString") method prints just the string "aString", but does not move the cursor to a new line. Hence, subsequent printing instructions will print on the same line.

The println("aString") method prints the string "aString" and moves the cursor to a new line.

The println() method can also be used without parameters, to position the cursor on the next line.

Regards

1

print() print which you want but in one line.

For example,

Serial.print("Hello");

Then output in Serial Monitor look like,

HelloHelloHelloHelloHello

println() print same things but in new line.

For example,

Serial.println("Hello");

Then output in Serial Monitor look like,

Hello
Hello
Hello
Hello
Hello

protected by VE7JRO Jul 28 at 7:50

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.