Multiple Inclusion of Standard Header Files

D

David T. Ashley

Are the standard header files (<stdio.h> for 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?
 
I

Ian Collins

David said:
Are the standard header files (<stdio.h> for 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.
 
G

Guest

David said:
Are the standard header files (<stdio.h> for 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 deï¬nition of NDEBUG (see 7.2). [...]"
 
S

sam_cit

David said:
Are the standard header files (<stdio.h> for 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...
 
R

Richard Heathfield

(e-mail address removed) said:
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_HEADER
#define H_CUSTOMER_HEADER 1
....
#endif

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

Malcolm McLean

Richard Heathfield said:
(e-mail address removed) said:


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_HEADER
#define H_CUSTOMER_HEADER 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
 
R

Richard Heathfield

Malcolm McLean said:

#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. :)
 
S

Serve Laurijssen

Richard Heathfield said:
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 :)
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top