473,769 Members | 4,470 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multiple Inclusion of Standard Header Files

Are the standard header files (<stdio.hfor example) always safe to include
more than once?

Is this just good programming practice on the part of the average person who
writes these things, or is this guaranteed in a standard?

------------------------------------------------------------
David T. Ashley (dt*@e3ft.com)
http://www.e3ft.com (Consulting Home Page)
http://www.dtashley.com (Personal Home Page)
http://gpl.e3ft.com (GPL Publications and Projects)
Jan 14 '07 #1
7 1759
David T. Ashley wrote:
Are the standard header files (<stdio.hfor example) always safe to include
more than once?
Yes
Is this just good programming practice on the part of the average person who
writes these things, or is this guaranteed in a standard?
It is - 7.1.2 paragraph 4.

--
Ian Collins.
Jan 14 '07 #2
David T. Ashley wrote:
Are the standard header files (<stdio.hfor example) always safe to include
more than once?

Is this just good programming practice on the part of the average person who
writes these things, or is this guaranteed in a standard?
It's guaranteed, with one exception. 7.1.2p4 states:
"Standard headers may be included in any order; each may be included
more than once in a given scope, with no effect different from being
included only once, except that the effect of including <assert.h>
depends on the definition of NDEBUG (see 7.2). [...]"

Jan 14 '07 #3

David T. Ashley wrote:
Are the standard header files (<stdio.hfor example) always safe to include
more than once?
Actually, many header file uses pre-processor statement

#ifndef INCLUDE
#define INCLUDE 1
....
....
....
#endif

which would take care that contents of the header are not replaced
again by the pre-processor...

Jan 14 '07 #4
sa*****@yahoo.c o.in said:
>
David T. Ashley wrote:
>Are the standard header files (<stdio.hfor example) always safe to
include more than once?

Actually, many header file uses pre-processor statement

#ifndef INCLUDE
#define INCLUDE 1
...
...
...
#endif

which would take care that contents of the header are not replaced
again by the pre-processor...
Nor would any other header that used the same identifier. Fortunately, that
isn't how things are done.

You pick a *unique* name, e.g. something like:

#ifndef H_CUSTOMER_HEAD ER
#define H_CUSTOMER_HEAD ER 1
....
#endif

That way, you don't disable other headers further down the list.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 14 '07 #5

"Richard Heathfield" <rj*@see.sig.in validwrote in message
sa*****@yahoo.c o.in said:
>>
David T. Ashley wrote:
>>Are the standard header files (<stdio.hfor example) always safe to
include more than once?

Actually, many header file uses pre-processor statement

#ifndef INCLUDE
#define INCLUDE 1
...
...
...
#endif

which would take care that contents of the header are not replaced
again by the pre-processor...

Nor would any other header that used the same identifier. Fortunately,
that
isn't how things are done.

You pick a *unique* name, e.g. something like:

#ifndef H_CUSTOMER_HEAD ER
#define H_CUSTOMER_HEAD ER 1
...
#endif

That way, you don't disable other headers further down the list.
#ifndef foo_h
#define foo_h
/* foo.h - defines a wonderful function */
double foo(int x);
#endif

No need to define as 1
Jan 14 '07 #6
Malcolm McLean said:

<snip>
#ifndef foo_h
#define foo_h
/* foo.h - defines a wonderful function */
double foo(int x);
#endif

No need to define as 1
True enough. I do it anyway, against the possibility that I might one day
find a use for changing it to 2. :-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 14 '07 #7

"Richard Heathfield" <rj*@see.sig.in validwrote in message
news:ha******** *************@b t.com...
True enough. I do it anyway, against the possibility that I might one day
find a use for changing it to 2. :-)
Just thinking about it, defining as 1 helps makes it possible to use both

#if !foo_h
and
#ifndef

I never do it though :)
Jan 14 '07 #8

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

Similar topics

2
21095
by: Jochen Zeischka | last post by:
Hi everybody! I have a question concerning code organisation. Suppose I have the following header file: #ifndef SOME_NAME #define SOME_NAME namespace N { void F()
8
6431
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? What is the best followed approach?
14
1774
by: Fritz Foetzl | last post by:
I'm flummoxed. I'm a veteran C++ programmer from the Unix/Linux camp, trying to learn Visual C++. I'm trying to build a project in which I need to include one header in a couple of different files, but the classic multiple inclusion problem is biting me hard. The #ifndef..#define..#endif method doesn't seem to be working, although all the documentation I've read indicates that it should. As a small example, I have an empty console...
1
1431
by: mead | last post by:
This is from an article: "Given a.h and a.cpp files, a.h has been included as the first header file in a.cpp. This will make sure that a.h does not expect a certain header files to be included before a.h. As a.h has been included as the first file, successful compilation of a.cpp will ensure that a.h does not expect any other header file to be included before a.h. If this is followed for all classes, (i.e. x.cpp always includes x.h as...
6
1754
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 dependencies of these dependencies and so on. This is quite ugly. A start of an "Example.cpp" file could look like this
6
5207
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 !!
8
4806
by: ewpatton | last post by:
I have a header that is shared among different CPP files for constants. When Microsoft Visual C++ links the .obj files, it complains that these names are all duplicates. How can I get it to realize that these are coming from the same header and refer to the same things instead of making two different copies?
6
2610
by: vsgdp | last post by:
I was looking at some library code today and noticed something like this: // sublibrary.h // define some constants, enums, symbols #include "componentA.h" #include "componentB.h" #include "componentC.h"
4
2491
by: =?Utf-8?B?UmljaEI=?= | last post by:
I am trying to create a project using the ASP.NET AJAX accordion control. I would like to dynamically add panes to the control with a form template added when the pane is added. I have tried unsuccessfully in creating the whole pane as a user control and have succeeded in adding the pane and then dynamically adding the content which is a user control to the pane, dynamically within the page. However I would like to have a single pane...
0
9591
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
10225
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10001
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9867
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8880
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6676
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3573
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.