tags:

views:

870

answers:

1

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

I am confused about the purpose of this ..?

+14  A: 

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 prevent double declaration of any identifiers such as types, enums and static variables.

LiraNuna
Thank you for the quick answer :)
Asad Khan
My pleasure :) *15chrlmt*
LiraNuna
Mmm interresting. I once quited VC++ cuz it gave me errors about double defines. Nevermind I'm an Objective-C coder now =)
Time Machine
Koning Baard XIV: VC even has a `#pragma once` which does the same :-)
Joey
Also it prevents recursive inclusions... Imagine "alice.h" includes "bob.h" and "bob.h" includes "alice.h" and they don't have include guards...
Kevin D.
@Kevin: that is what I mean. I wanted to manipulate a form which was opened by the form to manipulate. It gaveme lots of errors and I didn't know what to do. I gave up =)
Time Machine