By Asad Khan

2009-10-31 10:06:34 8 Comments

I have been seeing code like this usually in the start of header files:


And at the end of the file is


What is the purpose of this?


@Sandeep_black 2018-02-13 06:37:16

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

#ifndef __COMMON_H__
#define __COMMON_H__
//header file content

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.

@LiraNuna 2009-10-31 10:09:58

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.

@Joey 2009-10-31 10:23:03

Koning Baard XIV: VC even has a #pragma once which does the same :-)

@Kevin Dungs 2009-10-31 10:39:56

Also it prevents recursive inclusions... Imagine "alice.h" includes "bob.h" and "bob.h" includes "alice.h" and they don't have include guards...

@user142019 2009-10-31 10:58:12

@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 =)

@Keith Thompson 2013-08-24 19:45:53

@Јοеу: #pragma once is not portable; the common #ifndef idiom is recommended.

@CIsForCookies 2016-01-03 12:34:15

What is the problem of including headers multiple times? assuming no recursive inclusion... Is it a problem of same name variables, or maybe just a problem of a larger exe file?

@Roy 2013-08-24 19:31:23

#ifndef <token>
/* code */
/* code to include if the token is defined */

#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

@Keith Thompson 2013-08-24 19:46:31

Identifiers starting with an underscore are reserved; you shouldn't define them yourself. Use something like #ifndef H_HEADER_NAME.

@Stu 2014-05-15 13:05:19

I know this is an old comment, but actually the underscore restriction only applies to "external identifiers" - identifiers that could end up in the compiled object's symbol table, i.e. global variables and function names. It does not apply to macro names.

@Will 2017-06-28 08:23:50

Is Stu's comment true? I just read… and now I am not so sure.

Related Questions

Sponsored Content

23 Answered Questions

35 Answered Questions

21 Answered Questions

[SOLVED] What is the "-->" operator in C++?

1 Answered Questions

[SOLVED] The Definitive C++ Book Guide and List

  • 2008-12-23 05:23:56
  • grepsedawk
  • 2070798 View
  • 4250 Score
  • 1 Answer
  • Tags:   c++ c++-faq

14 Answered Questions

[SOLVED] Why can templates only be implemented in the header file?

11 Answered Questions

35 Answered Questions

[SOLVED] Why is "using namespace std" considered bad practice?

10 Answered Questions

17 Answered Questions

[SOLVED] Why should C++ programmers minimize use of 'new'?

5 Answered Questions

[SOLVED] What is the copy-and-swap idiom?

Sponsored Content