574
votes

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

#endif

What is the purpose of this?

4
+1 - I too had same doubt, and got much more good answer here, may be useful for future visitors : stackoverflow.com/q/3246803/1134940Abid Rahman K
I want to add to this that you can also use #pragma once, that's all you have to do and it serves the same purpose as ifndef. For comparison of the two, see: stackoverflow.com/questions/1143936/…Dimension
Best to mention what a #pragma is: it activates a compiler-specific feature. Although #pragma once is very widely supported, it's nonstandard.Potatoswatter
@Dimension: GNU's own documentation (info cpp or look here) says "it is not recognized by all preprocessors, so you cannot rely on it in a portable program.". And GNU cpp optimizes the common and portable #ifndef idiom so it's as efficient as #pragma once.Keith Thompson
Some things to consider: Don't use a macro name starting with an underscore; such identifiers are reserved to the implementation. More subtly, #ifndef HEADERFILE_H can violate the implementation's namespace of the header name happens to start with E; identifiers starting with E and a digit or uppercase letter are reserved to <errno.h>. I suggest #ifndef H_HEADERFILE.Keith Thompson

4 Answers

601
votes

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.

46
votes
#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, #endif statement. #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
16
votes

This prevent from the multiple inclusion of same header file multiple time.

#ifndef __COMMON_H__
#define __COMMON_H__
//header file content
#endif

Suppose you have included this header file in multiple files. So first time __COMMON_H__ is not defined, it will get defined and header file included.

Next time __COMMON_H__ is defined, so it will not include again.

3
votes

They are called ifdef or include guards.

If writing a small program it might seems that it is not needed, but as the project grows you could intentionally or unintentionally include one file many times, which can result in compilation warning like variable already declared.

#ifndef checks whether HEADERFILE_H is not declared.
#define will declare HEADERFILE_H once #ifndef generates true.
#endif is to know the scope of #ifndef i.e end of #ifndef

If it is not declared which means #ifndef generates true then only the part between #ifndef and #endif executed otherwise not. This will prevent from again declaring the identifiers, enums, structure, etc...