what is the difference between string.h and cstring?

X

Xiaoshen Li

Dear All,

I saw some people using
#include <string.h>

some people using
#include <cstring>

What is the difference between the two? I can find the file string.h at
/usr/include. I don't know if cstring is a file and if so, where to find it.

It seems that there are also other similar things, like:
#include <iostream>

#include <iostream.h>

Thank you very much.
 
V

Victor Bazarov

Xiaoshen said:
I saw some people using
#include <string.h>

some people using
#include <cstring>

What is the difference between the two? I can find the file string.h at
/usr/include. I don't know if cstring is a file and if so, where to find
it.

C headers exist in C++ in the form <cxxxxxx> and the main difference is
that in the "no .h" form, all symbols (except macros) are declared in the
'std' namespace. The .h form supposedly brings both std:: and regular
symbols. However, as admitted many times by library implementors, it is
rather difficult to follow those requirements. So, often the resulting
implementation is kind of in reverse: <xxxxxx.h> has all symbols in the
global namespace and <cxxxxxx> has it in both global and 'std'. That's
my understanding of it, anyway.

I don't know where you can find <cstring> if it is a file. The Standard
does not require it to be a file. For all we know, including it in your
It seems that there are also other similar things, like:
#include <iostream>

#include <iostream.h>

This is actually a different story. <iostream> is a standard C++ header,
and <iostream.h> is some historical, pre-standard, abomination. You're
not supposed to use any of those.

V
 
M

marcas

Xiaoshen said:
Dear All,

I saw some people using
#include <string.h>

some people using
#include <cstring>

What is the difference between the two? I can find the file string.h at
/usr/include. I don't know if cstring is a file and if so, where to find
it.

cstring makes string.h accessable in namnespace std. cstring is located
in /usr/include/c++ in my case. If you can't find it, just look for it
by typing

locate cstring

It should be in a subdir of /usr/include


The file contains a comment, which might be useful:

/** @file cstring
* This is a Standard C++ Library file. You should @c #include this file
* in your programs, rather than any of the "*.h" implementation files.
*
* This is the C++ version of the Standard C Library header @c string.h,
* and its contents are (mostly) the same as that header, but are all
* contained in the namespace @c std.
*/


Hope that helps,
regards marcas
 
P

Pete Becker

Victor said:
C headers exist in C++ in the form <cxxxxxx> and the main difference is
that in the "no .h" form, all symbols (except macros) are declared in the
'std' namespace. The .h form supposedly brings both std:: and regular
symbols. However, as admitted many times by library implementors, it is
rather difficult to follow those requirements. So, often the resulting
implementation is kind of in reverse: <xxxxxx.h> has all symbols in the
global namespace and <cxxxxxx> has it in both global and 'std'. That's
my understanding of it, anyway.

Your understanding is correct. The safe rule is that if you want the
names to be in the global namespace, use <string.h>; if you want them to
be in std::, use <cstring>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top