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

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

I just put my #defines to separate the code to be compiled on 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 this, the error was fixed:

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

I'm just wondering why I got this error and why #defines are different?

Thank you very much,

+73
c
Nov 11 '09 at 10:09
source share
4 answers

If you are using C # ifdef syntax, remove the parentheses.

The difference between the two is that #ifdef can use only one condition,
and #if defined(NAME) can perform complex conventions.

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 
+109
Nov 11 '09 at 10:11
source share
 #ifdef FOO 

and

 #if defined(FOO) 

match,

but to do several actions at once, you can use certain ones, for example

 #if defined(FOO) || defined(BAR) 
+30
Oct 20 '12 at 13:00
source share

#ifdef checks if the macro was the specified name, #if evaluates the expression and checks the true value

 #define FOO 1 #define BAR 0 #ifdef FOO #ifdef BAR /* this will be compiled */ #endif #endif #if BAR /* this won't */ #endif #if FOO || BAR /* this will */ #endif 
+21
Nov 11 '09 at 10:23
source share

Try replacing #elif with #else , since I think that #elif followed only by #if , not #ifdef .

-6
Mar 05 '16 at 12:06 on
source share



All Articles