TITLE: Leading underscores in macro names PROBLEM: turlough@odyssey.ucc.ie (Turlough FitzPatrick) Reciently I have come accross two different styles for ifdef statments in header files. They were : 1) #ifndef FILENAME_H_ #define ... ... #endif 2) #ifndef _filename_h #define ... ... #endif My question is, are they both valid, RESPONSE: clamage@Eng.Sun.COM (Steve Clamage), 23 Sep 94 The rules for exactly what spellings are reserved to the implementation are very complicated. Besides the names in the standard headers (reserved under different circumstances depending on the kind of name and whether the header is included), names beginning with two underscores or an underscore and an uupercase letter are reserved to the implementation for use as macro names (among other things). The specific example "_filename_h" is not reserved by C or C++. However, there are other standards which may also apply on your system (such as POSIX) which may reserve additional categories of names. As a matter of defensive programming, I never define names beginning with an underscore in application programs. The odds are too great that it will collide with a name in a system header, no matter what the C or C++ language rules say. Macros are particularly insidious, since they do not repect scopes and can cause bizarre failures. RESPONSE: tgakem@ta0.chem.tue.nl (Eric Meijer) The preprocessor preprocesses the code before it gets to the compiler (at least this is the way it is defined, there are compilers that treat the code in one pass). This means the compiler proper never gets to see the preprocessor symbols, and certainly the linker will never see them. So IMHO leading underscores in preprocessor symbols cannot be a problem. RESPONSE: clamage@Eng.Sun.COM (Steve Clamage), 23 Sep 94 You need to think about this a little more. For one thing, many categories of names beginning with an underscore are reserved to the implementation, which is reason enough to watch out for them. But here is a concrete example: File /* stdio.h is portable, customized by commondefs.h */ ============ #include /* which defines _EOF_H */ #ifndef EOF #define EOF _EOF_H #endif ... Your own header file eof.h ========================== #ifndef _EOF_H #define _EOF_H 1 ... #endif Now you write this: #include #include "eof.h" ... Notice that the contents of eof.h will be skipped. Now suppose you included the files in the other order. Then in your code the standard symbol EOF will have your definition of 1. So when you write while( something != EOF ) { ... } your program will not have the correct behavior. The system libraray will have used a different value for EOF.