strcat appending multiple times when called in loop The strcat function is working fine when called for the first time but when the broker goes down and it's attempting to reconnect it is appending the value twice. I am not able to figure out the problem as i am writing C code for the first time.

void reconnect() {

  while (!client.connected()) {
    const char* cid = clientID.c_str();
    if (client.connect(cid)) {
       const char* sTopic = strcat("clm/",cid);
       client.publish(outTopic, sTopic);
       client.subscribe(sTopic);
    } else {
    // Wait 5 seconds before retrying
    for(int i = 0; i&lt;5000; i++){
      delay(1);
    }
  }
}


}

It is called in the loop method

void loop() {
  if (!client.connected()) {
    reconnect();
  }
}


The value of sTopic for the first time is clm/5C:CF:7F:3D:5B:F6 but when the broker restarts and the client connects again it is clm/5C:CF:7F:3D:5B:F65C:CF:7F:3D:5B:F6

What could be the reason for the same ?
Appends a copy of the source string to the destination string.


source

Congratulations, you've broken your string literal. Either stick to using String or make sure you allocate a string properly and then concatenate into that.