Serial.println for several variables separated by a space on a single line?

Hello,

I would like to print to the serial monitor three variables, separately by a single space. I have attempted to do it this way but i get an error.

Serial.println(gx + " " + gy + " " + gz);

The error when compiling:

exit status 1
invalid operands of types 'const char*' and 'const char [2]' to binary 'operator+'

I don't understand what i'm doing wrong. How can i print these 3 variables with a single serial.println?

Why is it important to be done with a single println?

(Hint: String - but let's not go there)

AWOL:
Why is it important to be done with a single println?

(Hint: String - but let's not go there)

For simplicity?

I'm wondering why it's not working when i use the following to add space in between the variables:

+ " "

And what would be the easiest substitution to make this work?

I already told you how to do it, but it uses the String class.
Which you don't want to use.
Sometimes, simplicity comes at a price.

Because the print method does not use the + operator to concatenate strings.

here are a couple ways to go:
Use multiple prints:

Serial.print(gx);
Serial.print(" ");
Serial.print(gy);
Serial.print(" ");
Serial.println(gz);

Use sprintf:

char buffer[20];
sprintf(buffer, "%d %d %d", gx,gy,gz)
Serial.println(buffer);

groundFungus:
Because the print method does not use the + operator to concatenate strings.

Ah, it makes sense now.

In your second example, is there a reason why you chose the buffer size as 20? Is it a common size or just a guess?

It's a close guess to the really needed 21 (3*6 + 2 + 1).

But from your snippet nobody is able to know the type of the variables,
so the format and the size could be wrong anyway.

In your second example, is there a reason why you chose the buffer size as 20?

Whandall has it right. I have no idea of the data types or how many characters are needed so 20 was a WAG (Wild Ass Guess). Once you know how many characters will be put into the buffer, add 1 for the terminating '\0'.

Note that sprintf() does not work with the float data type on Arduino. You can convert the floats to strings using dtostrf() and use %s (string) to insert the parameters.

sprintf(buffer, "%s %s %s", gxStr, gyStr, gzStr)

Or you could simply drop the ridiculous "it has to go on one line" requirement, and let the Print class do its thing.

I don't say that sprintf() is THE way, but A way. I would, most likely, use multiple prints.

When doing Serial.println(gx + " " + gy + " " + gz); do it this waySerial.println( (String)gx + " " + (String)gy + " " + (String)gz);I

.. but better still, don't do it that way.

sprintf() comes into its own when you need substring formatting, left-right justification, decimal precision, pre-fill characters and stuff like that,

do it this way

The Evils of Arduino Strings explains why that is not the way to go.

groundFungus:
The Evils of Arduino Strings explains why that is not the way to go.

I have not had any issue with using Strings. I have one project going on 11 months that uses Strings that has not had an issue. By using String.reserve() one can gain control of the String fragment memory.

The evil of Strings is in lack of proper String management.

I will concede that if used properly the String class can be used without problems. The issue is that in few if any uses of Strings that you see in code on the net are they properly used and we get the "my sketch quit working" posts.

The evil of Strings is in lack of proper String management.

And there is precious little that shows the proper String management.

lastchancename:
sprintf() comes into its own when you need substring formatting, left-right justification, decimal precision, pre-fill characters and stuff like that,

...but needs a bit of work (and memory) when you want to use floating-point variables.

Another option is to use a streaming library like Streaming | Arduiniana
With it, you can write Serial << gx << " " << gy << " " gz << endl;
all on one line

@Idahowalker, did you read the linked blog about all the traps of String?

the support of function sprintf takes 1 kB flash.

DBMcDonald:
Another option is to use a streaming library like Streaming | Arduiniana
With it, you can write Serial << gx << " " << gy << " " gz << endl;
all on one line

bit shift operator override. I never liked it