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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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
ifndeffails, resulting in a blank file.That prevents double declaration of any identifiers such as types, enums and static variables.