Combine/concat char arrays

After searching hours in the net, not founding a good solution I ask in this great forum here
I have
int a;
char a_a[5];
int b;
char a_b[5];
char c[10];

void foo()
{
a = 5;
b = 3;
itoa(a,a_a,10);
itoa(b,a_b.10;
c = a+ “:”+b; // does not work
Serial.println(c);// should show “5:3” just for debug
// I need the char C[10] otherwise i.e for output to MQTT and LCD Display
}

All I found goes with strings, isn’t there a simpler way like in PHP i.e.

Thanks for help

Correct – C strings do not support concatinating strings like this. (And I also think you meant a_a + ":" + a_b there). See snprintf() with “%s%s” as format specifier, strcat or strncat.

In fact you can optimize the two separate buffers away entirley by directly snprintf()-ing into the final buffer with %d to convert the ints to their string representations.

int a;
int b;
char c[10];

void foo()
{
   a = 5;
   b = 3;
   snprintf(c, sizeof(c), "%d:%d", a, b);
   Serial.println(c);// should show “5:3” just for debug
   // I need the char C[10] otherwise i.e for output to MQTT and LCD Display
}

The + operator for string concatination is available in the Arduino String class though, so basing everything of String would look like

int a;
String a_a;
int b;
String a_b;
String c;

void foo()
{
   a = 5;
   b = 3;
   a_a = String(a);
   a_b = String(b);
   c = a_a + ":" + a_b; // works
   Serial.println(c);// should show “5:3” just for debug
   //get pointer to raw C string / char*
   const char* c_rawstr = c.c_str();
}

As per Arduino docs.

String is however a heap-allocated memory structure and not very memory efficient (see here), so you should prefer to work with C strings and the functions from string.h in memory-constrained envrionments (and for stability).The snprintf() version above is more optimal.

great exactly what I need, I have to learn more about snprintf
Rainer