0

In the file file.h, following code is seen.

#ifndef FILE_H
#define FILE_H
 ...
 ...
#endif

QUESTION: Who generated FILE_H (is FILE_H called identifier?) What is this naming convention called? What I should read to understand more of this?

At the moment, I know this is called include guard, and stuff to do with preprocessor. But I can't seem to google futher. Any links would be highly appreciated.

Sida Zhou
  • 3,529
  • 2
  • 33
  • 48
  • ...what's the question? The *programmer* generated `FILE_H`. I don't think there's a name for an obvious naming convention. Perhaps we can create one? The Naming-Your-Include-Guards-After-The-File-Name convention work for you? – Cody Gray - on strike Mar 29 '13 at 08:16
  • I meant, we are writing `#ifndef FILE_H` instead of, lets say, `#ifndef "file.h"`. So who decided we should do this? And where is this documented? – Sida Zhou Mar 29 '13 at 08:50
  • It's just a standard C macro. You can't use quoted strings, the preprocessor works on symbolic constants. Any introductory C or C++ book should explain this in the first few chapters. Macros are the documented part. Using preprocessor macros to implement include guards is a convention or idiom. It just happens to be one that is utterly ubiquitous. – Cody Gray - on strike Mar 29 '13 at 22:25
  • possible duplicate of [Naming Include Guards](http://stackoverflow.com/questions/4867559/naming-include-guards) and [#include header guard format?](http://stackoverflow.com/q/314983) – Cody Gray - on strike Mar 29 '13 at 22:25
  • Ok thanks, exactly what I was searching for. – Sida Zhou Mar 30 '13 at 01:38

1 Answers1

0

Include guards are not actually a feature of the language itself, they're just a way to ensure the same header file isn't included twice into the same translation unit, and they're built from lower-level language features.

The actual features that make this possible are macro replacement (specifically object-like macros) and conditional inclusion.

Hence, to find out where the FILE_H comes from, you need to examine two things.

The first is the limitations imposed by the standard (C11 6.4.2 for example). In macro replacement, the macro name must be drawn from a limited character set, the minimum of which includes the upper and lower case letters, the underscore, and the digits (there are all sorts of extras that can be allowed, such as universal character names or other implementation-defined characters but this is the mandated baseline).

The second is the mind of the developer. Beyond the constraints of the standard, the developer must provide a unique identifier used for the include guard and the easiest way to do this is to make it reliant somehow on the file name itself. Hence, one practice is to use the uppercase file name with . replaced by an underscore.

That's why you'll end up with an include guard for btree.h being of the form:

#ifndef BTREE_H
#define BTREE_H
    // weave your magic here
#endif

You should keep in mind however that it doesn't always work out well. Sometimes you may end up with two similarly named header files that use the same include guard name, resulting in one of the header files not being included at all. This happens infrequently enough that it's usually not worth being concerned about.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953