user37570: 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<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 ?

user359: strcat won't allocate any memory for the new string. So what really happens is that the location where the string constant "clm/" is stored gets the id appended. 
In other words the second time around it's as if you passed "clm/5C:CF:7F:3D:5B:F6" as first parameter.
To fix that you need to supply a proper buffer for strcat to do its work: 
if (client.connect(cid)) {
   char[64] sTopic ={0};
   strcat(sTopic , "clm/");
   strcat(sTopic , cid);


user368: 
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.

37570: <p>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.</p>

<pre><code>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);
    }
  }
}
</code></pre>

<p>}</p>

<p>It is called in the loop method</p>

<pre><code>void loop() {
  if (!client.connected()) {
    reconnect();
  }
}
</code></pre>

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

<p>What could be the reason for the same ?</p>
 359: <p>strcat won't allocate any memory for the new string. So what really happens is that the location where the string constant <code>"clm/"</code> is stored gets the id appended. </p>

<p>In other words the second time around it's as if you passed <code>"clm/5C:CF:7F:3D:5B:F6"</code> as first parameter.</p>

<p>To fix that you need to supply a proper buffer for strcat to do its work: </p>

<pre><code>if (client.connect(cid)) {
   char[64] sTopic ={0};
   strcat(sTopic , "clm/");
   strcat(sTopic , cid);
</code></pre>
 368: <blockquote>
  <p>Appends a copy of the <em>source</em> string to the <em>destination</em> string.</p>
</blockquote>

<p><a href="http://www.cplusplus.com/reference/cstring/strcat/" rel="nofollow noreferrer">source</a></p>

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