I noticed that there was (at least on Mac OS X) both a <string.h>
header and a <strings.h>
header. man 3 string
reveals that they contain different functions. Is there any reason for this?
Asked
Active
Viewed 5.5k times
99
-
2For what it's worth, OS X `strings.h` contains nonstandard functions `bcmp bcopy bzero ffs index rindex strcasecmp strncasecmp`… and that's it. – Potatoswatter Nov 27 '10 at 10:28
-
4@Potatoswatter: It's doing exactly what's specified by POSIX. – R.. GitHub STOP HELPING ICE Nov 27 '10 at 14:18
2 Answers
121
strings.h comes from the BSD branch in the unix evolution. Its content has been standardized by POSIX, but most of it is marked as legacy and can be easily replaced with other functions:
int bcmp(const void *, const void *, size_t); /* LEGACY, see memcmp */
void bcopy(const void *, void *, size_t); /* LEGACY, see memcpy, memmove */
void bzero(void *, size_t); /* LEGACY, see memset */
int ffs(int);
char *index(const char *, int); /* LEGACY, see strchr */
char *rindex(const char *, int); /* LEGACY, see strrchr */
int strcasecmp(const char *, const char *);
int strncasecmp(const char *, const char *, size_t);

AProgrammer
- 51,233
- 8
- 91
- 143
-
7Some C standard libraries have merged the non-deprecated functions of `strings.h` into `string.h`. See, e.g., [Glibc](http://www.gnu.org/software/libc/manual/html_mono/libc.html#String_002fArray-Comparison). – entropo Apr 28 '11 at 15:34
-
One important function from strings.h that now exists in glibc string.h is `explicit_bzero`. e.g. https://github.com/bminor/glibc/blob/master/string/explicit_bzero.c – angstyloop Aug 13 '22 at 22:32
21
Typically <strings.h>
just adds some useful but non-standard additional string functions to the standard header <string.h>
. For maximum portability you should only use <string.h>
but if you need the functions in <strings.h>
more than you need portability then you can use <strings.h>
instead of <string.h>
.

Paul R
- 208,748
- 37
- 389
- 560
-
2I would question the description of these functions as "useful". Most of them are ugly BSD duplicates of standard ANSI/ISO C functions with different names. The case-insensitive comparison functions for byte strings are (in a cross-platform reliability sense) probably not useful on modern UTF-8 strings, and even if they "work", they probably don't provide the semantics the programmer wants. Only `ffs` is possibly useful. – R.. GitHub STOP HELPING ICE Nov 27 '10 at 14:20
-
4@R.: they are useful if you have legacy BSD code to compile that uses these functions. ;-) – Paul R Nov 27 '10 at 14:22