Skip to content

Instantly share code, notes, and snippets.

@WoodyWoodsta
Last active October 27, 2015 09:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WoodyWoodsta/44b5d232375f0dfb3d76 to your computer and use it in GitHub Desktop.
Save WoodyWoodsta/44b5d232375f0dfb3d76 to your computer and use it in GitHub Desktop.
Multiple inclusion guard tactic for C explained
/** To prevent your code having multiply included header files,
* and hence possibly multiply declared functions and variables,
* the following tactic is used in the file to be included which will
* prevent the code from being compiled more than once.
*/
// The "ifndef" combined with the matching "endif" directives serve as a pre-processor
// "if not defined" statement, where the code between these two directives will only
// Be compiled if, in this case, the symbol "NAME_OF_HEADER_FILE_H" has not been defined.
#ifndef NAME_OF_HEADER_FILE_H
#define NAME_OF_HEADER_FILE_H // This defines the symbol, which means that the "ifndef" directive in all
// other multiply included files will evaluate to false, and the code within
// it and the "endif" will not be compiled! Problem solved.
// == Header contents ==
#endif // This is the matching directive that encloses the code to be either compiled or ignored
// NOTE: that this is not the only use of the #define directive. There are a huge amount of other use cases!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment