title: Concatenating strings in C: string buffer is not getting cleared after function call
body: <p>I am trying to concat two strings in the function <code>foo()</code>. I expect the content of <code>string</code> to be <code>abc</code> after both the first and second calls to <code>foo()</code>. The output which I get after the first call is <code>abc</code> but the second call prints <code>abcabc</code> (the third call prints <code>abcabcabc</code> and so on). The string buffer is not getting cleared. It will be helpful if someone could share how it works internally - how the memory is assigned to the string and why the string buffer is not cleared between subsequent calls of the function.</p>
  <p>Here is the code I used:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  int foo(){
      unsigned char string[100];
      unsigned char text[10] = &quot;abc&quot;;
      strcat(string, text);
      printf(&quot;%s\n&quot;, string);
  }
  
  int main(){
      foo();
      foo();
      return 0;
  }
  </code></pre>