Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

best-practice on C header files with #ifndef #define #endif

what is concerned best practice regarding the following "pattern"?

#ifndef BLAFOO_H
#define BLAFOO_H
/* ...
 * ...
 */
#endif /* BLAFOO_H */

how should i name the header in the #define directive? i've seen all from said BLAFOO_H to __BLAFOO_H to _BLAFOO_H_ etc..

like image 695
guest Avatar asked May 30 '10 16:05

guest


People also ask

What should be included in C header file?

The header file contains only declarations, and is included by the . c file for the module. Put only structure type declarations, function prototypes, and global variable extern declarations, in the . h file; put the function definitions and global variable definitions and initializations in the .

Should every C file have a header file?

Generally it's best to have a header file for each . c file, containing the declarations for functions etc in the . c file that you want to expose.

Does order of header files matter in C?

The GNU C Library header files have been written in such a way that it doesn't matter if a header file is accidentally included more than once; including a header file a second time has no effect. Likewise, if your program needs to include multiple header files, the order in which they are included doesn't matter.

Where should header files be included?

Header files should #include the minimum header files necessary, and source files should also, though it's not as important for source files. The source file will have the headers it #include s, and the headers they #include , and so on up to the maximum nesting depth.


2 Answers

Name them BLAFOO_H (personnally I use BLAFOO_H_ where BLAFOO is the header file name ).

Make sure your BLAFOO doesn't clash with other files/libraries/etc. you're using, e.g. have your project and/or module name be parth of that name.

Identifiers starting with a _ is reserved for the implementation/compiler, so don't use that.

like image 121
nos Avatar answered Oct 17 '22 07:10

nos


I use an UUID that is my guarantee that #define not clashed with others. I've seen it somewhere and decided to use it as well.

My pattern is like this: __<filename>_H_<uuid>__,

eg. #define __TYPES_H_79057761_16D6_478A_BFBC_BBF17BD3E9B9__ for a file named types.h

like image 21
Aoi Karasu Avatar answered Oct 17 '22 07:10

Aoi Karasu