Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between <string.h> and <strings.h>

Tags:

c

string

header

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?

like image 393
icktoofay Avatar asked Nov 27 '10 10:11

icktoofay


People also ask

Is it string H or strings H?

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.

Is string and string h the same?

<string. h> contains old functions like strcpy , strlen for C style null-terminated strings. <string> primarily contains the std::string , std::wstring and other classes.

What is meant by string H?

h is the header in the C standard library for the C programming language which contains macro definitions, constants and declarations of functions and types used not only for string handling but also various memory handling functions; the name is thus something of a misnomer. Functions declared in string.

Is string H necessary?

You don't need to include any headers for it. But there is a possibility to avoid using <string. h> for certain compilers. See commit, which fixed compiler errors for cross-compiling on android, which added <string.


2 Answers

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); 
like image 100
AProgrammer Avatar answered Oct 09 '22 11:10

AProgrammer


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>.

like image 23
Paul R Avatar answered Oct 09 '22 11:10

Paul R