Reputation: 9002
What is the difference between string.h
and cstring
?
Which one should be used for C and which one for C++ (if at all)?
Upvotes: 30
Views: 39129
Reputation: 21
<string.h>
is C Header ,used for manipulation of NULL Terminated Character Array ~ Loosely string .
<string>
is C++ provides which has more convenient and advanced manipulation of strings (characters).
<cstring>
is C++ header providing backward combability with C type Strings.
<cstring>
provide c string function in function Manner like
string1 = string2.c_str();
Also has useful/basic/vintage functions like strcpy(), strlen() provided <string.h>
C style which is by seceded by C++ style copy() , size() .
<string.h>
is declared in
Global NameSpace
meanwhile ,
<cstring>
is declared in
Standard (std) NameSpace
.
Upvotes: 2
Reputation: 133
While using <cstring>
, some answers suggest using std::
namespace. However, the code will still compile and run without the std
namespace. This is because:
<cstring> = "string.h" + <functions defined in the std namespace>
This was done to maintain compatibility with C.
Try it yourself by including <cstring>
and calling both memcpy()
and std::memcpy()
.
Upvotes: 0
Reputation: 206526
In C++ you should include cstring
as the header while in c you should include string.h
as the header.
In C++
#include <cstring>
In C
#include <string.h>
Features of C standard Library are also provided in the C++ Standard library and as a general naming convention they are pre-pended by an c
to the corresponding names in C standard library.
For Example:
string.h
becomes cstring
stdio.h
becomes cstdio
and so on...
Since other answers have added different dimensions to this discussion,I felt compelled to refer the holy standard to clear this bit.
As per C++11 20.9.14.6 & 7:
Table 55 describes the header
<cstring>
.
The contents are the same as the Standard C library header , with the change tomemchr()
specified in 21.7.
While 21.7 Null-terminated sequence utilities states:
The function signature
memchr(const void*, int, size_t)
shall be replaced by the two declarations:const void* memchr(const void* s, int c, size_t n); void* memchr( void* s, int c, size_t n);
both of which shall have the same behavior as the original declaration.
Annex D (normative) Compatibility features [depr] states:
D.6 C standard library headers
1 For compatibility with the C standard library and the C Unicode TR, the C++ standard library provides the 25 C headers, as shown in Table 151.
Which include:
<assert.h> <float.h> <math.h> <stddef.h> <tgmath.h>
<complex.h> <inttypes.h> <setjmp.h> <stdio.h> <time.h>
<ctype.h> <iso646.h> <signal.h> <stdint.h> <uchar.h>
<errno.h> <limits.h> <stdarg.h> <stdlib.h> <wchar.h>
<fenv.h> <locale.h> <stdbool.h> <string.h> <wctype.h>
Further on,
2 Every C header, each of which has a name of the form
name.h
, behaves as if each name placed in the standard library namespace by the correspondingcname header
is placed within the global namespace scope. It is unspecified whether these names are first declared or defined within namespace scope (3.3.6) of the namespace std and are then injected into the global namespace scope by explicit using-declarations (7.3.3).3 [ Example: The header
<cstdlib>
assuredly provides its declarations and definitions within the namespace std. It may also provide these names within the global namespace. The header<stdlib.h>
assuredly provides the same declarations and definitions within the global namespace, much as in the C Standard. It may also provide these names within the namespace std. —end example ]
From the above references:
I stand corrected on my earlier suggestion, there seems to be no apparent advantage of using cstring
over string.h
while as @Alf suggested there might be some compilation issues due to use of unqualified function names when using cstring
as header. So given hat there is no apparent dis-advantage of using string.h
or advantage of using cstring
, I think either can be used in C++ if used in a proper manner.
Upvotes: 31
Reputation: 14632
Apparently cstring
is for C++ and string.h
is for C.
One thing worth mentioning is, if you are switching from string.h
to cstring
, remember to add std::
before all your string function calls.
Upvotes: 2
Reputation: 184
You can use string.h for both C & C++.
In C++ 98 spec, it define both the cstring (in main spec) and string.h (in Annex D.5, Standard C library headers, for compatibility), which define some string function the same as string.h in C. And in real world, all C++ compiler will provide string.h for compatibility to C code.
So, in my opinion, as C++ code maybe maintain by C coder, and the habit from C, I prefer string.h. It's clear enough, wide known, and more compatibility (with C).
BTW, I list the all 18 headers in C++ for compatibility with C, in C++ 98 spec: assert.h, iso646.h, setjmp.h, stdio.h, wchar.h, ctype.h, limits.h, signal.h, stdlib.h, wctype.h, errno.h, locale.h, stdarg.h, string.h, float.h, math.h, stddef.h, time.h
Upvotes: 5
Reputation: 88155
The C++ version of the header actually has some differences from the C version. In C some types are implemented as typedefs, but for C++ that prevents things like template specialization from working on those types*, so C++ makes some C typedefs into real types. This means that the C++ version of C headers that contain those typedefs must omit them.
C++ also allows overloading and so the C++ version of <cstring>
specifies some overloads to C functions to allow a function to return a pointer to non-const data if the input argument is a pointer to non-const data, whereas the C function takes and returns only pointers to const.
Also I think, but can't find the bit in the standard to verify this right now, that the C++ versions of headers have to put their names in the std namespace and only put them in the global namespace as an optional extension.
* For example the following code:
typedef int Foo;
template<typename T> struct Bar {};
template<> struct Bar<int> {};
template<> struct Bar<Foo> {};
results in the following error:
main.cpp:7:19: error: redefinition of 'Bar<int>'
template<> struct Bar<Foo> {};
^~~~~~~~
main.cpp:5:19: note: previous definition is here
template<> struct Bar<int> {};
^
1 error generated.
In C wchar_t is a typedef, so this would prevent one from having a specialization that applies to wchar_t but not to whatever underlying type is used for wchar_t. The fact that MSVC 2010 implements char32_t and char16_t as typedefs prevents them from being able to offer the std::codecvt specializations for those types.
Upvotes: 2
Reputation: 9002
There is a subtle difference between string.h and cstring
Answer of Alf P. Steinbach (can be found as a comment to the asked question):
string.h
places the identifiers in the global namespace, and may also place them in the standard namespace. While cstring
places the identifiers in the standard namespace, and may also place them in the global namespace. You definitely don't want that cstring
behavior, because code that e.g. uses just strlen
may work fine with one compiler, then fail to compile with another compiler. It's very unpleasant surprise. So for C and C++, use the more safe string.h
.
Upvotes: 7
Reputation: 4025
In C++, C language headers are defined under the namespace std. So, if you are using those headers in C++, use cstring and eliminate .h .
Upvotes: 4