Konrad
Konrad

Reputation: 40947

Difference between preprocessor directives #if and #ifdef

What is the difference (if any) between the two following preprocessor control statements?

#if

and

#ifdef

Upvotes: 64

Views: 29347

Answers (2)

Greg Hewgill
Greg Hewgill

Reputation: 992747

You can demonstrate the difference by doing:

#define FOO 0
#if FOO
  // won't compile this
#endif
#ifdef FOO
  // will compile this
#endif

#if checks for the value of the symbol, while #ifdef checks the existence of the symbol (regardless of its value).

Upvotes: 134

ereOn
ereOn

Reputation: 55726

#ifdef FOO

is a shortcut for:

#if defined(FOO)

#if can also be used for other tests or for more complex preprocessor conditions.

#if defined(FOO) || defined(BAR)

Upvotes: 40

Related Questions