What is the difference between print( ) and println( )?
I see none when I run a code.
- 111
- 1
- 1
- 6
4 Answers
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
_
- 81
- 1
-
1Your 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
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.
- 339
- 1
- 11
-
Could you post your code please, as my answer is kind of incomplete now. – Mathsman 100 Apr 19 '15 at 11:37
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
- 121
- 1
- 5
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
- 1,476
- 14
- 28
lnfromprintlncomes fromline, meaning it will print a new line character at the end – rslite Apr 19 '15 at 14:43