Difference between Serial.print(" ") and Serial.print(' ')

Hi, as subject suggests, what is the difference between Serial.print(" ") and Serial.print(' ')? I've noticed that Serial.print(' ') returns some numbers, but I don't know where they come from.

Whenever you use double-quote marks, it is assumed you are printing out a sequence of characters (i.e., a string). For this to work correctly, the last character should be a (non-printing) null termination character, '\0'. Any time a pair of single quotes are used, you are referencing a single character. The numbers refer to the ASCII character set, where each character is assigned a number. For example, the letter 'A' is ASCII code 65.

This short program might help:

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  Serial.println(65);           // Print the integer number 65
  Serial.println( (char) 65);   // Change the number 65 to an ASCII character and print it
  Serial.println("A");          // Print a string with the letter A in it.
}

void loop

I've noticed that Serial.print(' ') returns some numbers, but I don't know where they come from.

It returns the number of characters it has printed.

AWOL:
It returns the number of characters it has printed.

I tried Serial.println('hello'); and it returned number 27759.

luckyx182:
I tried Serial.println('hello'); and it returned number 27759.

That is the decimal value of 0x6C6F which is an integer containing the characters 'l' and '0'. An integer can only contain two characters so the others are ignored.

johnwasser:
That is the decimal value of 0x6C6F which is an integer containing the characters 'l' and '0'. An integer can only contain two characters so the others are ignored.

That isn't what I'd call "the number of characters printed"... though I guess you could parse that sentence that way. The way you'd expect from that phrasing, ie, that if you print 10 characters, it would return 10, sounds a lot more useful.... feature or bug?

johnwasser:
That is the decimal value of 0x6C6F which is an integer containing the characters 'l' and '0'. An integer can only contain two characters so the others are ignored.

That was helpful, I get it, thank you. But it seems strange to me that it takes the last two characters of it not the first two.

luckyx182:
I tried Serial.println('hello'); and it returned number 27759.

It didn't return the number 27759 it printed the number 27759.

luckyx182:
But it seems strange to me that it takes the last two characters of it not the first two.

If I try to stuff a big number into a small one, like this:

  unsigned int a = 396352345;

That truncates the higher-order bits.

So you will see more like the "345" at the right-hand end than the "396" at the left. Of course, it won't be exactly that because this is in binary, not decimal.

In this case, if you printed "a" you would see 56153.

 396352345 modulo 65536 = 56153

I know it doesn't return the number, my bad. Just didn't know what word to use.

Thanks Nick, your explanation makes perfect sense. :wink:

I don’t want to seem pedantic, but just in case it wasn’t clear:

void setup ()
  {
  Serial.begin (115200);
  int i = Serial.print ("42");  // <--- prints 42, returns 2
  Serial.println ();
  Serial.println (i);
  }  // end of setup
void loop ()
  {
  }  // end of loop

In that sketch:

  int i = Serial.print ("42");

The Serial.print prints 42 but it returns 2. The number 2 returned is the number of bytes printed. The number 42 printed is what you asked it to print.

That is the decimal value of 0x6C6F which is an integer containing the characters 'l' and '0'. An integer can only contain two characters so the others are ignored.

I am wondering why only two characters are contained? Is that because Serial.print() only prints up to an integer?

chrisplusian:
That is the decimal value of 0x6C6F which is an integer containing the characters 'l' and '0'. An integer can only contain two characters so the others are ignored.

I am wondering why only two characters are contained? Is that because Serial.print() only prints up to an integer?

All characters are integers. You mean up to an "int".