Is it possible to print a mix of string and integer?

I am new at this so bare with me.

Trying to print to monitor (Serial.println) a mix of text and an integer.
Is it possible.
I know it is possible in a clean C with println("text text &d", int); but it fails.
Using a nano.

thanks

Peter

Is it possible.

Yes

int anInt = 123;
Serial.print("Number ");
Serial.print(anInt);
Serial.println(" on the screen");

Or use the sprintf() sledgehammer

char buffer[30];
int anInt = 123;
sprintf(buffer, "Number %d on the screen", anInt);
Serial.println(buffer);

Thanks

I have tried the first solution but the result in my mac is the printed in three lines.
Will try your other solution now.

great forum

Peter

Yes the second solution worked perfectly.

thanks

Peter

@PICT - nothing to do with your Mac, I'm 99,99% sure you ended up with 3 lines because you used print[color=red]ln[/color] instead of print. The [color=red]ln[/color] piece in the method call means print the text followed by a carriage return character (ASCII 13, or '\r') and a newline character (ASCII 10, or '\n').

I see.

Yes I used Serial.println after watching a lot of tutorials where they only used that command when printing to screen..
But that is very useful information.

thanks a lot

Peter

pict:
Yes the second solution worked perfectly.

thanks

Peter

Be aware that it uses a lot more memory than the first one.
Even better use

Serial.print(F("Number "));
Serial.print(anInt);
Serial.println(F(" on the screen"));

UKHeliBob:
Be aware that it uses a lot more memory than the first one.
Even better use

Serial.print(F("Number "));

Serial.print(anInt);
Serial.println(F(" on the screen"));

Yes I tried it and it used less memory.

Thanks all of you

Peter