14

Possible Duplicate:
Difference between string.h and cstring?

What is better programming practice in C++ when including the standard header files with respect to

including cmath in place of math.h or vice-versa?

including cstring in place of string.h or vice-versa?

and for other <c*> and <*.h> header files which apparently seem to accomplish the same thing?

Community
  • 1
  • 1
smilingbuddha
  • 14,334
  • 33
  • 112
  • 189

1 Answers1

24

<cstring> is newer; <string.h> is really there for backwards compatibility (and for C, of course). The difference is that <cstring> puts the string functions in the std namespace, while <string.h> puts them in the global namespace.

In addition, <cstring> changes the types of certain functions to promote type-safety. E.g., the C declaration

char *strchr(char const *, int);

is replaced by the overloads (in the std namespace)

char       *strchr(char       *, int);
char const *strchr(char const *, int);

In the case of <cmath> there are further differences with <math.h> which make <cmath> more idiomatic and less C-like.

Prefer <cstring> for new code and use the std:: prefix on the functions.

Community
  • 1
  • 1
Fred Foo
  • 355,277
  • 75
  • 744
  • 836