Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C Preprocessor testing definedness of multiple macros

I searched the site but did not find the answer I was looking for so here is a really quick question.

I am trying to do something like that :

#ifdef _WIN32 || _WIN64      #include <conio.h> #endif 

How can I do such a thing? I know that _WIN32 is defined for both 32 and 64 bit windows so I would be okay with either for windows detection. I am more interested in whether I can use logical operators like that with preprocessor directives, and if yes how, since the above does not work.

Compiling with gcc I get :

warning: extra tokens at end of #ifdef directive , and it basically just takes the first MACRO and ignores the rest.

like image 373
Lefteris Avatar asked Jun 08 '09 16:06

Lefteris


People also ask

Are macros removed after preprocessing?

Explanation: True, After preprocessing all the macro in the program are removed.

What is difference between preprocessor and macros?

Macro: a word defined by the #define preprocessor directive that evaluates to some other expression. Preprocessor directive: a special #-keyword, recognized by the preprocessor. Show activity on this post. preprocessor modifies the source file before handing it over the compiler.

Which of the following directive processor will return true if this macro is defined?

#ifdef: It returns true if a certain macro is defined. #ifndef: It returns true if a certain macro is not defined. #if, #elif, #else, and #endif: It tests the program using a certain condition; these directives can be nested too.

Can a macro call another macro in C?

Short answer yes. You can nest defines and macros like that - as many levels as you want as long as it isn't recursive.


2 Answers

Try:

#if defined(_WIN32) || defined(_WIN64) // do stuff #endif 

The defined macro tests whether or not a name is defined and lets you apply logical operators to the result.

like image 175
Aaron Maenpaa Avatar answered Oct 21 '22 14:10

Aaron Maenpaa


You must use #if and special operator defined

like image 23
cube Avatar answered Oct 21 '22 15:10

cube