#ifndef, #define and #endif are preprocessor directives. Any line of code starting with a # is for the preprocessor.
The #ifndef tests if a symbol is not defined. If the symbol is not defined (no #define for it), then the #ifndef is true and the preprocessor continues until it reaches a #endif.
So this code:
- #ifndef ABC
-
#define ABC
-
-
struct X
-
{
-
-
};
-
#endif
when encountered the first time the ABC symbol is not yet defined. So the #ifndef is true and the lines inside the #ifndef are processed. In this case, the ABC symbol is defined and the struct X is accepted.
The next time this #ifndef is encountered, ABC is defined so the preprocessor skips to the #endif.
The effect is that struct X is processed only once. This a voids a redefinition error should struct X be encountered more than once.
This allows you to #include the same header file more than once in the same source file without causing an error.
Hence, this collection of macros is called an
inclusion guard.