I am not a big fan of the C/C++ preprocessor directives
I am not denying that they certainly have their place and usage in the language.
I'll first write about where I think they are useful and then about the situations
where I feel they are not.
#ifndef is very useful for preventing multiple
inclusions of a header which will cause redefinition errors.
#ifndef __FOO_H__ // only proceed if the token is not yet defined
#define __FOO_H__ // define the token so that subsequent includes have it defined
[...contents of the header...]
#endif // __FOO_H__, end for #ifndef statement
Of course, you can use
#pragma once to do the same thing, but that is a
Microsoft specific extension to the language so not everyone will use it. This
usage is error prone though. I can't count the number of times that I copied one
header to create another header in the same project and forgot to change the
#ifndef token to a new token and couldn't
figure out why my new header wasn't being evaluated properly.
#ifdef is useful in a header to switch between
ANSI and wide versions of the function, such as this example from
#define CreateFile CreateFileW
#define CreateFile CreateFileA
#endif // !UNICODE
Now, the reason I don't like these 2 directives is that they behave differently
#if token evaluates the value of
token is not defined, it defaults to zero and the
#if token phrases evaluates to zero. On the other hand,
#ifdef token evaluates to token to see if it is
defined, regardless of the value of
token. That means that the following code
#define FOO 0x0
A lot of you may not agree with me and I am fine with that ;). Please let me
know why though.