The difference between preprocessor directives #if and #ifdef

What is the difference (if any) between the next two preprocessor control operations.

#if 

and

 #ifdef 
+49
c-preprocessor
Sep 27 '10 at 10:34
source share
2 answers

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 the value of a character, and #ifdef checks for the presence of a character (regardless of its value).

+109
Sep 27 '10 at 10:50
source share
 #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) 
+36
Sep 27 '10 at 10:35
source share



All Articles