0

Possible Duplicate:
Difference between <string> and <string.h>?

My specific example uses following clause:

#include <string>

If I use following clause instead

#include <string.h>

compiler ends with error

[BCC32 Error] utils.cpp(173): E2316 'getline' is not a member of 'std'

Line 173 in utils.cpp file is as follows:

while(std::getline(in, line, '\n'))

I thought that there is no difference between these two clauses. Now I am confused. What files are in fact included by these two clauses? Lets say, my C++ Builder installation has program directory C:\Program Files\RAD Studio\9.0 and include files are located in subdirectory C:\Program Files\RAD Studio\9.0\include.

Community
  • 1
  • 1
truthseeker
  • 1,220
  • 4
  • 25
  • 58
  • 7
    These are two different headers. `string.h` is from C library. – jrok Jul 10 '12 at 11:42
  • @jrok, why not post that as an answer?? – hmjd Jul 10 '12 at 11:43
  • 1
    @hmjd I'm going for Pundit badge :P Really, I wanted to search for duplicates first. – jrok Jul 10 '12 at 11:45
  • Iran, Iraq - what's the difference? – Tadeusz Kopec for Ukraine Jul 10 '12 at 12:01
  • I formulated my question more generally than linked question. I did not find it because it was to specific. It can happen that someone in future will have the same kind of problem with another set of headers like for example memory.h and memory. Should he open new question? – truthseeker Jul 10 '12 at 13:53
  • @truthseeker, `` and `` are not related either. Maybe your general question would have been clearer if you didn't give a specific example showing a compilation failure that is entirely due to those specific headers being unrelated. The answer to your general question of "what's the difference?" is simply they're two different files, which may or may not be related. In the examples you've given, they're not related. – Jonathan Wakely Jul 10 '12 at 14:23

3 Answers3

8
#include <string>

This includes the C++ string header.

#include <string.h>

This includes the C string header, with all identifiers in the global namespace. (Deprecated.)

#include <cstring>

This includes the C string header, with all identifiers placed in the std:: namespace.

Edit: Rule of thumb - C++ headers never end on ".h". Prefix the traditional C header name with "c" and drop the ".h" to keep the global namespace clean. Use ".h" for your project's C headers only. Use ".hpp" for C++-only headers.

DevSolar
  • 67,862
  • 21
  • 134
  • 209
2

They are two different headers. The convention in the C standard library is to have the headers ending with .h, whereas in the C++ standard library the convention is to miss out the file extension altogether. Some more detail from wikipedia:

Each header from the C Standard Library is included in the C++ Standard Library under a different name, generated by removing the .h, and adding a 'c' at the start; for example, 'time.h' becomes 'ctime'. The only difference between these headers and the traditional C Standard Library headers is that where possible the functions should be placed into the std:: namespace (although few compilers actually do this). In ISO C, functions in the standard library are allowed to be implemented by macros, which is not allowed by ISO C++.

Other libraries follow different conventions. Boost, for instance, chooses .hpp as their C++ header extension of choice.

Alex Wilson
  • 6,690
  • 27
  • 44
-2

By convention, C (Procedural) headers ends by '.h' "string.h", "stdio.h"... and C++ (Object oriented mostly) don't include any extension: "iostream", "string" ...

Not sure if all headers follow this convention, but I think that the standards ones all do.

Samy Arous
  • 6,794
  • 13
  • 20
  • Not all C++ headers deal with OO code. – Benjamin Bannier Jul 10 '12 at 11:48
  • No. All C++ Standard headers have no extension, and all C headers are `.h`. The usual extension for C++ is `.hpp` or `.hh`. – Puppy Jul 10 '12 at 11:49
  • @Honk: 1st: I said mostly, 2nd: All the standard headers I know of present at least one object class or are related to such objects: string, vector, algorithms... – Samy Arous Jul 10 '12 at 11:55
  • @DeadMG: How is this different from what I said? By definition a convention is not a rule, and nothing stops you from using C++ class in a .h header. – Samy Arous Jul 10 '12 at 11:57
  • 1
    Talking about C++ as "OO", or even "OO mostly", is sadly missing the point of what is, very fundamentally, a *multi-paradigm* language. – DevSolar Jul 10 '12 at 11:57
  • 1
    The extension of the headers has *nothing to do with the paradigm of the code contained within*. – Puppy Jul 10 '12 at 12:05
  • which objects or OO things are defined in ``? – Jonathan Wakely Jul 10 '12 at 12:42
  • @JonathanWakely, well a `grep` for `struct` on stl_algo.h and stl_algobase.h return something here under gcc-4.3.2, but wait ... – Benjamin Bannier Jul 10 '12 at 13:57
  • Internal implementation details defined as static member functions of structs do not mean something is OO. I didn't ask about a specific implementation, I asked about ``. The answer is it defines `std::initializer_list`, by including ``, and `std::pair` by reference, but that doesn't count as OO in my book. – Jonathan Wakely Jul 10 '12 at 14:20