I have been seeing code like this usually in the start of header files:
#ifndef HEADERFILE_H #define HEADERFILE_H
And at the end of the file is
What is the purpose of this?
Those are called #include guards.
Once the header is included, it checks if a unique value (in this case
HEADERFILE_H) is defined. Then if it's not defined, it defines it and continues to the rest of the page.
When the code is included again, the first
ifndef fails, resulting in a blank file.
That prevents double declaration of any identifiers such as types, enums and static variables.
#ifndef <token> /* code */ #else /* code to include if the token is defined */ #endif
#ifndef checks whether the given token has been
#defined earlier in the file or in an included file; if not, it includes the code between it and the closing
#else or, if no
#else is present,
#ifndef is often used to make header files idempotent by defining a token once the file has been included and checking that the token was not set at the top of that file.
#ifndef _INCL_GUARD #define _INCL_GUARD #endif