Take the 2-minute tour ×

How come this code


compiles with (I'm using MSVC 2010)

#include <string>

but not with

#include <string.h>


share|improve this question

8 Answers 8

up vote 21 down vote accepted
  • string.h contains old functions like strcpy, strlen.
  • string primarily contains the std::string class.
share|improve this answer
It should also be noted that using string.h is deprecated within C++. If you need the functionality contained within, you should use the header cstring. This more or less completely bypasses the issue of "What's the difference between these two" because it's very obvious that one is from the C library. –  Mike Bantegui Mar 11 '12 at 20:53

string.h is a C header not a C++ header, period!

share|improve this answer
Do you mean that when I do a couple of files .h/.cpp I should remove the .h or is your answer just for euh, like sdk files? –  Valmond Feb 13 '12 at 9:05
I think, it's better to say the other way: <string> is C++ header, not a C header. –  iammilind Feb 13 '12 at 9:06

string.h is C's header file while string is C++'s header file.

share|improve this answer
<string.h> can be considered C++ header file also. :) –  iammilind Feb 13 '12 at 9:06

<string.h> is cstring - http://www.cplusplus.com/reference/clibrary/cstring/

<string> is the c++ string class - http://www.cplusplus.com/reference/string/

Edit per Nicol Bolas comment below and a bit of googling:

<cstring> will usually import the same things as <string.h> but into the std namespace. <string.h> will usually import everything into the global namespace. It appears to depend on the library implementation you're using though according to my googling.

Personally I only ever use <cstring> if I need C style string helpers.

share|improve this answer
<cstring> is not a synonym for <string.h>. cstring puts stuff in the std namespace (it may also leave them outside of the std namespace), while <string.h> does not. –  Nicol Bolas Feb 13 '12 at 8:53
Using C style .h headers is deprecated in C++. –  Prasoon Saurav Feb 13 '12 at 8:56
@PrasoonSaurav To be deprecated it would have to have been part of the standard to begin with, which it never was. –  Peter Wood Feb 13 '12 at 9:52
@PeterWood : Check out Annex D [D.5] C++03. –  Prasoon Saurav Feb 13 '12 at 10:10
@PrasoonSaurav Thank you, I was unaware of that. –  Peter Wood Feb 14 '12 at 8:50

<string.h> contains C-library string functions. strlen, strcmp, etc.

<string> contains the definition for std::basic_string, which has the typedefs std::string and std::wstring. That's the difference.

They really have no relationship at all, outside of the fact that they both deal with strings.

share|improve this answer

They are entirely different headers.

<string> is C++ string class

<string.h> or <cstring> defines functions to manipulate C strings and arrays

share|improve this answer

I believe <string.h> is just used for C and <string> for C++. So including string.h wont work.

share|improve this answer

As stated, string.h and cstring are C headers (while cstring is basically a C++ wrapper for string.h), containing functions for C strings, which are char[] terminated by '\0'. You want to use the c++ class string, which header is <string>.

share|improve this answer

Your Answer


