The argument for including the included guards in a file that contains a header, and not in the header itself, is that if the file is already included, the compiler (in particular, the preprocessor) should not open and read the file again.
This is a weak argument. In practice, the time saved is trivial, and the probability of error is high.
In your example:
#ifndef HEADER_H #include "header.h" ... #endif
you are not showing us #define HEADER_H . Is it somewhere in header.h ? If so, how do you know that the author of header.h decided to use HEADER_H as the name of the included security macro? What if he changes to something else later?
If you decide to include the include security element in the include file, you must also define a macro:
#ifndef HEADER_H #include "header.h" #define HEADER_H #endif
But, as the other answers have already said, it is much better to put the guard in the header:
header.h:
#ifndef HEADER_H #define HEADER_H #endif
and then include just has:
#include "header.h"
and has less information to worry about.
Keith thompson
source share