Ask HN: Preferred string copy function in C?

1 points by codehero ↗ HN
1. strcpy

2. strncpy

3. stpcpy

4. memcpy

4 comments

[ 3.7 ms ] story [ 111 ms ] thread
Depending on your requirements, each has its benefits and drawbacks. Depending on your architecture, there are versions that are faster than the libc version.

None of the functions you have listed are true equivalents, so, your situation will dictate which you need to use.

strcpy and stpcpy are functionally equivalent, but differ only in return value. stpcpy is superior because it points to the end of the string, thus letting the caller know how many bytes were copied.
strlcpy
strdup is nice, but it always depends on your use case.

memcpy is probably best if you're keeping track of lengths separately.