title: c strncpy null terminated or not
body: <p>I am reading <a href="http://www.cplusplus.com/reference/cstring/strncpy/" rel="nofollow noreferrer">this</a> document, it says:</p>
  
  <blockquote>
  <pre><code>char *strncpy(char *destination, const char *source, size_t num);
  </code></pre>
    
    <p>Copy characters from string
     Copies the first <code>num</code> characters of <code>source</code> to <code>destination</code>. If the end of the <code>source</code> C string (which is signaled by a null-character) is found before <code>num</code> characters have been copied, <code>destination</code> is padded with zeros until a total of <code>num</code> characters have been written to it.</p>
    
    <p>No null-character is implicitly appended at the end of destination if source is longer than <code>num</code>. Thus, in this case, <code>destination</code> shall not be considered a null terminated C string (reading it as such would overflow).</p>
    
    <p><code>destination</code> and <code>source</code> shall not overlap (see <code>memmove</code> for a safer alternative when overlapping).</p>
  </blockquote>
  
  <p>But I am confused by this statement:</p>
  
  <blockquote>
    <p>in this case, destination shall not be considered a null terminated C string (reading it as such would overflow)</p>
  </blockquote>
  
  <p>Since if <code>num &gt; strlen(source)</code>, it will pad with <code>'\0'</code> at the end, <code>'\0'</code> is actually a null (terminating) character in a string, why it shall not be considered a null-terminated C string?</p>
  
  <p>I have written below code to verify:</p>
  
  <pre><code>  char from[] = { 'h', 'e', 'l', 'l', 'o', '\0' };
    char to[1024];
    for (int i = 0; i &lt; 1024; i++) {
        to[i] = 'e';
    }
    strncpy(to, from, 1024);
    printf("from %s\n", from);
  </code></pre>
  
  <p>It works fine with below output:</p>
  
  <pre><code>from hello
  to hello
  </code></pre>