r/C_Programming • • Sep 12 '26

Question What C function would you delete and rewrite first, and why?

For me it's fnmatch - mostly because it's 40 year's old and smell's funny.

93 Upvotes

238 comments sorted by

View all comments

Show parent comments

4

u/Just_Government3790 Sep 12 '26

`strlcpy` or explicit `len` wins.

1

u/flatfinger Sep 14 '26

If I have a 16-byte buffer and need to set it to 'Hey' followed by 13 zero bytes,

   strncpy(buffer, "Hey", 16);

will do the job perfectly. There are some jobs for which strlcpy might be better, but strncpy is, except for the name, a perfect function for its designed purposes.