30

What is the difference between string.h and cstring?

Which one should be used for C and which one for C++ (if at all)?

12
  • 2
    C and C++ are not the same language. Since you mention cstring I'm assuming you mean C++ and have removed the C tag. Commented Dec 5, 2011 at 3:54
  • 1
    @BrianRoach mm.. not quite, I wanted to have a comparison, what header should be used for which language. Commented Dec 5, 2011 at 3:55
  • 2
    that makes no sense since cstring can't be used in C. Commented Dec 5, 2011 at 3:57
  • 7
    Oh wow ... googling cstring really improved my evening ... Commented Dec 5, 2011 at 3:59
  • 1
    @EtiennedeMartel - you mean about googling? Try it and erm ... scroll down to the images part. I seriously had no idea that was an actual thing (and I'm no prude). Commented Dec 5, 2011 at 4:07

8 Answers 8

31

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 to memchr() 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 corresponding cname 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 ]

Conclusion:

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.

Sign up to request clarification or add additional context in comments.

10 Comments

@julio.alegria No. No no no no no.
I meant, it's possible (just re-checked)
@julio.alegria: It is possible does not mean it is correct, is an important trait of C++ programming language that you should learn about.
@Als et al: using [cstring] as an alternative to [string.h] has no advantages and some severe disadvantages, like code failing to compile with other compiler. So in the either-or view, the only sane choice is [string.h]. I'm not sure if it has any practical adantage to include both. Maybe. But for just 1, then [string.h]. Cheers,
@Nicol: I already gave an example. But anyway, code that uses any [string.h] function unqualified, such as strlen, may compile with [cstring], and probably will compile. For in C++11 [cstring] is allowed to litter the global namespace at will. With some other compiler and a cleaner [cstring] then the conventional (should I say habitual?) call fails to compile. With C++03 [cstring] was not allowed to pollute the global namespace in this way, and that was the whole point of the header. However, AFAIK nobody implemented that, so that it remained completely pointless... Pretty ironic.
|
7

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.

9 Comments

because code that e.g. uses just strlen may work fine with one compiler, then fail to compile with another compiler. And I've found code that compiles under Visual Studio without warning or error will fail in GCC, because some VS headers will implicitly include things like memcpy while GCC won't. So I don't see how improper use of a header is a reason to pick one over the other; you're not guaranteeing yourself anything. If you include the c* version, then you should be using the std:: namespace qualified names. And if you're not, then you get what you deserve.
@NicolBolas You certainly have more expertise then me. I think the advantage of using string.h is just to be on the safe side. Yes, this will put all symbols of string.h in the global namespace, but the same can happen with cstdio. So why bother?
Did you see bames53's answer?
NicolBolas Only now... This seem strange to me. How could @AlfP.Steinbach be so sure?
@Beginner: because the ISO C++ standard says so. barnes53 writes, correctly, that "The C++ version of the header actually has some differences from the C version.". That refers to C++'s [string.h], not a difference between [cstring] and [string.h]. Cheers & hth.,
|
5

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

2 Comments

"In C++ 98 spec, it only define the cstring" happily that's incorrect
oh yes. It mentioned at Annex D.5, Standard C library headers. I will fix the post.
4

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 .

3 Comments

"In C++, C language headers are defined under the namespace std" <- that's incorrect
@Alf P. Steinbach, I thought the c-style libraries are implemented under std namespace in C++. This link says so as well cplusplus.com/reference/clibrary
You can use that site as quick-help thing, but not as a reference. Besides, the link you refer to says "Every element of the library is defined within the std namespace" about the cxxxx headers, which is almost correct, but is not what you think it says. The C++ standard is the reference. The current standard is C++11. It's not cheap, but happily the N3290 draft is identical and was public a short time (if you should happen to get it later, like now, it would be illegal, he he).
2

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.

2 Comments

"prevents things like template specialization from working on those types" << Could you give an example of what you're trying to say here? On the surface of it, it doesn't make sense.
Thanks for the example, now I understand what you mean. E.g. that in C++ wchar_t is a built-in type. Regarding the global namespace thing, see my answer. Cheers,
2

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.

Comments

2

<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

.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.