sieg1974 said:
Tom Zych <
[email protected]> wrote in message
sieg1974 said:
I have made a program that is divided in many group of functions, and
each of these groups has its .h and .c files ( for example dutils.h
and dutils.c ). And to avoid the same .h file to be include more than
once, I wrote these command everywhere where I need to include
something.
#ifndef H_DUTILS
# define H_DUTILS
# include "dutils.h"
#endif
Is it a good way to do it, or there is a better way?
A better way is to put the same kind of logic inside the header file
itself:
#ifndef DUTILS_H /* note changed macro name */
#define DUTILS_H
[the rest of the header file here]
#endif
Then you only need #include "dutils.h" in the other files.
But what would happen if I had two or more .c files that would include
stdio.h for example?
Then the text from the stdio header will be pasted into
each .c file. Note that this will create two separate
translation units. The header is not being #included
more than once.
What I think you might have meant to ask is what
happens if #include <stdio.h> appears more than
once in the same source file.
Answer:
If the implementation is compliant The Right Thing(tm)
will happen.
Does stdio.h have these ifndef, define, endif
inside it?
It might.
Or will it just be included more than once?
The standard only stipulates that #including
a standard header more than once has the
same effect as if #included only once (with
one exception, see below). How this is achieved
is up to the implementation, but using 'include
guards' is common.
C&V:
=============================================================
ISO/IEC 9899:1999 (E)
[...]
7.1.2 Standard headers
[...]
4 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). If used, a header shall be included
outside of any external declaration or definition, and it
shall first be included before the first reference to any
of the functions or objects it declares, or to any of the
types or macros it defines. However, if an identifier is
declared or defined in more than one header, the second and
subsequent associated headers may be included after the
initial reference to the identifier. The program shall not
have any macros with names lexically identical to keywords
currently defined prior to the inclusion.
=============================================================
-Mike