difference between #if defined(WIN32) and #ifdef(WIN32)

c
ant2009 picture ant2009 · Nov 11, 2009 · Viewed 93.8k times · Source

I am compiling my program that will running on linux gcc 4.4.1 C99.

I was just putting my #defines in to separate the code that will be compiled on either windows or linux. However, I got this error.

error: macro names must be identifiers.

Using this code

#ifdef(WIN32)
/* Do windows stuff
#elif(UNIX)
/* Do linux stuff */
#endif

However, when I changed to this the error was fixed:

#if defined(WIN32)
/* Do windows stuff
#elif(UNIX)
/* Do linux stuff */
#endif

I was just wondering why I got that error and why the #defines are different?

Many thanks,

Answer

user44556 picture user44556 · Nov 11, 2009

If you use #ifdef syntax, remove the brackets.

The difference between the two is that #ifdef can only use a single condition,
while #if defined(NAME) can do compound conditionals.

For example in your case:

#if defined(WIN32) && !defined(UNIX)
/* Do windows stuff */
#elif defined(UNIX) && !defined(WIN32)
/* Do linux stuff */
#else
/* Error, both can't be defined or undefined same time */
#endif