473,395 Members | 1,521 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

header file inclusion in c++

Hi,

I often include the header files like the following,

#include <string.h>

However, i get a warning from the compiler to remove the .h and it
works fine with the following,

#include <string>

I also read an article which suggests to do the same, however it
doesn't claim any reason to do so... does anyone have any idea
regarding the same?

Thanks in advance!!!
Dec 23 '07 #1
5 1750
"Rahul" <sa*****@yahoo.co.inwrote in message
news:b2**********************************@e6g2000p rf.googlegroups.com...
Hi,

I often include the header files like the following,

#include <string.h>

However, i get a warning from the compiler to remove the .h and it
works fine with the following,

#include <string>

I also read an article which suggests to do the same, however it
doesn't claim any reason to do so... does anyone have any idea
regarding the same?
The C++ standard library includes the (C90) standard C library,
so headers from both languages are available. The C++ library also
provides 'new' forms of the C headers, with the same names prepended
with letter 'c', and the '.h' dropped. The original names (with .h)
are also included for compatibility.

So. e.g. you can #include <stdio.hor <cstdiofor the same features.
Also the C names are encapsulated in namespace 'std' just as are the
C++ names (except macros).

Note that <string.h(and <cstring>) declare the 'C-style' string
operations (e.g. strcpy(), and <stringdeclares the C++ std::string type
and associated functions. Not the same thing at all.

Your questions cause me to ask which book(s) you're using to learn
C++ that don't explain this.

-Mike
Dec 23 '07 #2
On 2007-12-23 19:07, Mike Wahler wrote:
"Rahul" <sa*****@yahoo.co.inwrote in message
news:b2**********************************@e6g2000p rf.googlegroups.com...
>Hi,

I often include the header files like the following,

#include <string.h>

However, i get a warning from the compiler to remove the .h and it
works fine with the following,

#include <string>

I also read an article which suggests to do the same, however it
doesn't claim any reason to do so... does anyone have any idea
regarding the same?

The C++ standard library includes the (C90) standard C library,
so headers from both languages are available. The C++ library also
provides 'new' forms of the C headers, with the same names prepended
with letter 'c', and the '.h' dropped. The original names (with .h)
are also included for compatibility.

So. e.g. you can #include <stdio.hor <cstdiofor the same features.
Also the C names are encapsulated in namespace 'std' just as are the
C++ names (except macros).
Just to clarify, if you include the C++version of a C header (e.g. one
of those starting with c and without the .h) the functions are placed in
the std namespace, if you include the C version of the header (those
with .h) they are in the global namespace.

Also, AFAIK, the C++ headers (those not also in C) have never ended in
..h in standard C++, however before C++ was standardised using .h was
common so many compiler supports it for backwards compatibility.

--
Erik Wikström
Dec 23 '07 #3
On Dec 23, 1:07 pm, "Mike Wahler" <mkwah...@mkwahler.netwrote:
Note that <string.h(and <cstring>) declare the 'C-style' string
operations (e.g. strcpy(), and <stringdeclares the C++ std::string type
and associated functions. Not the same thing at all.
I think this point could use a little expansion. Since the OP was
asking about <string.hvs. <string>, it should be pointed out that
this is an STL-specific requirement and the info provided about
standard C libs in C++, though useful, is slighty OT. Circa 1999, STL
dropped all the .h's from its include files and put them in an 'std'
namespace (std::vector, std::list, std::map, std::queue, etc.).

--Jonathan
Dec 23 '07 #4
Rahul wrote:
Hi,

I often include the header files like the following,

#include <string.h>

However, i get a warning from the compiler to remove the .h and it
works fine with the following,

#include <string>

I also read an article which suggests to do the same, however it
doesn't claim any reason to do so... does anyone have any idea
regarding the same?

Thanks in advance!!!
As others are explaining, <string.his the c-style string functions. That
has been renamed to <cstringin the latest version of c++. <stringis the
c++ style string STL std::string. However, <stringalso includes some of
the c-style string functions also (not sure if it includes them all or not).

In either case, you would want to use <stringor <cstring>, but not the
depreciated <string.h>.

I'm just trying to point out that <stringis not the same as <string.h>,
but <cstringis the same as <string.halthough you'll probably need to use
the std:: prefix.

--
Jim Langston
ta*******@rocketmail.com
Dec 23 '07 #5
On Dec 23, 5:48 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
As others are explaining, <string.his the c-style string functions. That
has been renamed to <cstringin the latest version of c++. <stringis the
c++ style string STL std::string. However, <stringalso includes some of
the c-style string functions also (not sure if it includes them all or not).
Well, I would assume that since his compiler said to replace
<string.hwith <string(and presumably, the code still compiled)
that he was only using STL string functionality and the C-style
functions that might have been included as a side effect. So, the
most direct answer to his question:
I also read an article which suggests to do the same, however it
doesn't claim any reason to do so... does anyone have any idea
regarding the same?
would be that STL dropped the .h's from its include files circa 1999.
This other C-style information that you guys have provided is just a
bonus IMHO. :-)
Dec 24 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: qazmlp | last post by:
I need to include a list of - C++ headers - headers of other modules - headers of my module - implementation specific ie.OS headers In what order, they should be included in my .CPP file?...
31
by: Steven T. Hatton | last post by:
If a header is not necessarily a source file, and the sequences delimited by < and > in header names aren't necessarily valid source file names, what exactly is a header? -- p->m == (*p).m == p.m...
6
by: Johannes Bauer | last post by:
Hi group, I've got a question concerning inclusion of .hpp files. Currently I'm including all needed header files in the .cpp file. This means all dependencies of the package and all...
18
by: Exits Funnel | last post by:
Hello, I'm a little confused about where I should include header files and was wondering whether there was some convention. Imagine I've written a class foo and put the definition in foo.h and...
18
by: John Smith | last post by:
Hi all What does the group think of the practise of including one header file from inside another? I have some legacy code where this has been done, and it creates a dependency on a module...
8
by: nrhayyal | last post by:
Hi c++ Gurus, Need your blessing. while testing few aspects with respect to header file inclusions, i observed few things which i would like to share with you. i have a file sqlca.h in which a...
6
by: techBoy | last post by:
I am looking for a tool that can scan my soyrce code and check if a header file gets included more then once in a sequece of compiled code. Can some one guide me to such a tool !!
3
by: fc2004 | last post by:
Hi, Is there any tools that could report where cyclic header dependency happens? this would be useful when working with a large project where tens or hundreds of headers files may form complex...
4
by: Christoph Scholtes | last post by:
Hi, I have some questions about header files: Say I have a file functions.c which contains a couple of functions. I have declared some structs in this file too. The structs are defined in...
16
by: wdh3rd | last post by:
Hi everyone. I'm new to C and I have a few questions: I am making files for permutations and combinations. Files to be made are perm.c, perm.h, combo.c, and combo.h. Since both combinations...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.