Is it possible to put different instructions into a Serial.println line?

int a=1;
void setup() 
{
  Serial.begin (9600);
}
void loop() 
{
  Serial.print("a= ");
  Serial.println(a);
}

Works OK but is it possible to change the two lines to one with something like

Serial.print("a= " a );

matelot:

int a=1;

void setup()
{
  Serial.begin (9600);
}
void loop()
{
  Serial.print("a= ");
  Serial.println(a);
}




Works OK but is it possible to change the two lines to one with something like



Serial.print("a= " a );

Try it.

LOL.

Have a look at the sprintf() function, but why bother ?

I have been trying it and similar for hours now.

I get
unable to find string literal operator 'operator'"a'

when I do it exactly this way.

If I add a colon, semi colon, comma or other way I get other errors showing up.

No, that won't work.

If you've been programming on more powerful platforms, you're probably very used to the concept of building up a string, and then sending it to a device all at once. It's great practice there, often making code easier to read. On embedded platforms, with limited memory, it's not such a good idea, because you need to use precious bytes of RAM to store that buffer - sure, you can do it with sprintf(), or even with String class (which you should avoid because it is very easy to get yourself in trouble with due to memory fragmentation and the fact that the amount of memory it needs is not known at compile time, making it much easier to unexpectedly run out of memory at runtime. Remember - on an Uno/Nano/ProMini you have 2048 bytes of ram; on a desktop PC, the system usually has at least 4 GB of ram - two million times as much as the arduino, and nobody would blink at the code on a webpage using a couple MB at a time.

Other than making the code look less ugly, there's no benefit to combining the data to print in a single call to Serial.print() or Serial.println() - the overhead in terms of code size is much smaller than you'd think.

So it's very common (and in well written arduino code) to see things like:

Serial.print("It has been ");
Serial.print((millis()/1000));
Serial.println(" seconds since startup");

to give a trivial example

This works:

int a=1;
void setup() 
{
  Serial.begin (9600);
}
void loop() 
{
  Serial.print("a= "); Serial.println(a);
}

I generally use this form to debug variables.

I thought I had seen it somewhere.
My program includes a number of instances of the two lines so to shorten it so both instructions were in the one set of brackets would save space.

so to shorten it so both instructions were in the one set of brackets would save space.

Only in the editor

If you are printing a text label and the value of a variable you could put the multiple lines in a function and call it with the text and variable to be printed as parameters.

Can you give some examples of what you are printing.

matelot:
I thought I had seen it somewhere.

Maybe on an arduino DUE, because this sketch works fine:

int32_t a = 1, b=2, c=3;

void setup() {
 
Serial.begin(250000);
printf("a= %d, b= %d, c= %d\n", a,b,c);

/* Outputs on a DUE:
 a= 1, b= 2, c= 3
 */

}

void loop() {

}

matelot:
so to shorten it so both instructions were in the one set of brackets would save space.

Saving space in the way you write code is completely unnecessary. Write the code in the way that makes it easiest to read and understand after being away from it for 6 months.

The compiler is very clever at eliminating waste and minimizing the hex code that is uploaded to the Arduino.

Saving space in the Arduino’s SRAM can be important. For example the F() macro tells the compiler to store fixed text in PROGMEM rather than SRAM

…R

DrAzzy:
So it's very common (and in well written arduino code) to see things like:

Serial.print(F("It has been "));
Serial.print((millis()/1000));
Serial.println(F(" seconds since startup"));

to give correct a trivial example