There is occasionally a good reason to use strncpy()
. However:
Using
strncpy()
into a large buffer can be very inefficient.strncpy()
always writes to every byte in the destination buffer, which can waste a lot of time if the destination buffer is much longer than the source string.I once made critical code several times faster simply by recognizing that the previous programmer had copied short strings with
strncpy (a, b, 4096);
and fixing it.If the source string is longer than the size of the destination buffer, then
strncpy()
doesn't write a terminating null. So a call tostrncpy()
must be followed by explicitly writing a null terminator at the end of the destination buffer in most cases.