Add string to array


I want to add the following string

version = "v1.00";


char msg = "Arduino" + version;

How should I write the expression

Hello and welcome :slight_smile:

There are few functions that you can use,

For adding at the end of a char array (what you want to do), use strcat
For advanced formatting, use sprintf

I want to

'char msg " shall include the value

Arduino v1.00

Well I gave you 2 ways to make it, now click on the links and study :stuck_out_tongue:

I can not get it to work

Can you give me an example?


I can not get it to work

What have you tried?

What type is version?

The msg array does not have room for more characters.

If it did, as in:

char msg[80] = "Hello, ";

and the part you wanted to append to it was properly defined, as in:

char add[] = "world!";


strcat(msg, add);

would do the trick. After that call, msg contains "Hello, world!".

I have the line:

char msg = "The temperature is";

and want to add a string variable

float temperature = getTemp ();

the result I will have

char msg = "Temperature is: 24.5";

Use dtostrf to convert the float into a char array.

char msg[24] = "Temperature is: ";
char temperatureStr[7];
dtostrf( temperature, 0, 1, temperatureStr );
strcat( msg, temperatureStr );


char msg[24];
dtostrf( temperature, 0, 1, msg );
sprintf( msg, "Temperature is: %s", msg );

There is another solution:

char msg[24];
sprintf( msg, "Temperature is: %.1f", temperature );

But this requires that you do some modifications: install this in \hardware\tools\avr\avr\lib (make a backup of that lib folder, and then extract and replace files).


Sorry I have not heard of me

It works well