Considering this code:
limit = sizeof(str1)-strlen(str1)-1;
strncat(str1,str2,limit);
If str2 length is greater than limit, does strncat Nul terminates str1 or I have to add this code, like in the case of strncpy?
str1[sizeof(str1)-1] = '\0'
It always null-terminate.
Quoting C11, chapter §7.24.3.2, (emphasis mine)
The
strncatfunction appends not more thanncharacters (a null character and characters that follow it are not appended) from the array pointed to bys2to the end of the string pointed to bys1. The initial character ofs2overwrites the null character at the end ofs1. A terminating null character is always appended to the result.
and, the footnote
Thus, the maximum number of characters that can end up in the array pointed to by
s1isstrlen(s1)+n+1.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With