22

Possible Duplicate:
What is the difference between using #include<filename> and #include<filename.h> in c++

I've never noticed it making any difference whether or not I include the .h at the end of an include, so I've always ignored the meaning, but I've just noticed in a particular program of mine, I get the error "memcpy was not declared in this scope" if I include "string", but not if I include "string.h".

First of all, I was wondering the specific cause of this, but also generally the difference between the two. At the same time, if someone could explain the difference between includes in angular brackets and those in quotation marks, It'd be much appreciated.

Community
  • 1
  • 1
wyatt
  • 3,188
  • 10
  • 36
  • 48

5 Answers5

35

<string> is the C++ standard library string header file containing std::string and its friends. <string.h> is a different header, from the C standard library, which has functions for manipulating C strings (null-terminated strings) and other related functions.

The two are entirely different and unrelated. In C++ (as in C), a header file can have any extension. The C++ standard library headers have no extension; the C standard library headers have a .h extension. .hpp or .hxx are also common.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • 2
    And also note that (I believe) even though the include directive is `` or whatever, the compiler isn't required to actually call the corresponding file `string`. – Mark B May 10 '10 at 00:20
  • @Mark B: Indeed; it doesn't even have to be a file at all. I don't know of any implementations that don't use files with the names of the headers; I think I'd find that quite confusing! – James McNellis May 10 '10 at 00:22
  • 3
    Everything James said is true. It's also interesting that in updated standard C++, the C string file has been renamed to - in fact all of the C standard library, when promoted to the C++ standard library, have been renamed with a 'c' prefix and no '.h' extension, i.e., cstdlib, cstdio, etc. It's supposed to be the case that the C functions are moved into the std namespace, as well. Many compilers continue to support , et al., and not to put the C standard library in std::, but I believe they are not required to do either anymore. – jwismar May 10 '10 at 00:27
  • @jwismar: All implementations are required to provide both the `` and `` headers; the `` headers, as you say, put all their non-macro names into namespace `std`, and the `` headers put all their non-macro names into the global namespace. In many implementations, the `` headers also put their names into the global namespace; this is technically not allowed in C++03, but will be permitted in the forthcoming C++0x. – James McNellis May 10 '10 at 00:32
6

I'll just add, that in C++ there are C headers available in a traditional C form like string.h and those are used like #include <string.h>, but there are also their counterparts with the names starting with the letter "c" and without an extension - like cstring. Those headers are used like C++ headers #include <cstring> and the names from those headers are in the std namespace.

Maciej Hehl
  • 7,895
  • 1
  • 22
  • 23
3

They are 2 different files and have nothing to do with each other.

#include <string> refers to the C++ standard library STL strings

#include <string.h> defines several functions to manipulate C strings and arrays.

No extension is assumed for includes.

Some header files don't have .h appended to them. You commonly see this with header files that contain templates.

Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
1

Concerning the difference between using angular brackets and quotation marks, i think it has something to do with the order in which the header files are looked for : if it's between quotation marks, the compiler will first check the working directory (not very useful in the case of system headers, since they're not in the same directory as the compiler) before checking the system headers directory.

Shelldon
  • 84
  • 4
1

About the difference between includes in angular brackets and those in quotation marks, the first instructs the preprocessor to search for include files first along the path specified by the /I compiler option. The second tells the preprocessor to look for include files in the same directory of the file that contains the #include statement.

Gianluca
  • 805
  • 1
  • 10
  • 20