compiler directives : testing a macro value

Hi,

is there a way to test the value of macro #define ? The classic technique is to test whetehr something is defined for not:

#define debug

#ifdef debug 
serial.print(x); 
#endif

what I would like to do is more like :

#define debug_level 2

#ifdef debug _level == 2 
serial.print(x); 
#endif

I have searched and not been able to find any solution but neither have I found anywhere where someone asked and was told it was not possible, so many be I'm just not using the right words or something.

It seems too obvious and useful, I'd be surprised if does not exist but I can't see how to do it nor can I find a solution.

Could someone tell me if this simply not possible or if there is a trick?

Thanks.

#ifdef means if defined

if you want to test values, you use #if - Here is an example

#define debug 2

#if debug > 2
#define printdebug Serial.println("DEBUG > 2")
#else
#define printdebug Serial.println("DEBUG <= 2")
#endif

void setup() {
  Serial.begin(115200);
  printdebug;
}

void loop() {}

configured this way the console will display "DEBUG <= 2" and if you set debug to 5, then you'll see the other message

if you studied some "old" libraries they have been modified at some point to accommodate for Arduino.h and they start with

#if defined(ARDUINO) && ARDUINO >= 100
  #include "Arduino.h"
#else
  #include "WProgram.h"
#endif

this tests the version number of ARDUINO and if it's greater than 100 (or equal) then it includes Arduino.h otherwise WProgram.h

wow, that was fast.

don't know why I was not able to find that through big G. probably too busy trying to sell me something.

Thanks.

I think this is a good overview of preprocessor directives:
http://www.cplusplus.com/doc/tutorial/preprocessor/
It can be very difficult to use search engines for some programming topics because they like to ignore certain characters. I find that extremely frustrating and have not discovered a solution after quite a bit of research. I want to be able to search for an exact term rather than some fuzzy results that assume I'm an idiot. Of course idiot mode is a nice option too since I notice typos in my quick search queries quite often, but I don't want it forced on me.

typing "c++ preprocessor directives" in google gives you plenty to learn from...

this is the first hit for me - not too bad

thanks Pert, that looks like a useful, Bookmarked.

@ J-M-L, yes , as always it's case of getting the right words. I was thinking "C language" rather than c++ and probably stuck on "define" rather than your more generic term and I was thinking compiler directives not preprocessor directives.

Searching for C does not work because G just ignores it. You then get left with "language" which is a total red herring.

As I suspected , it's just a case of not getting the right words in there.

when you search weird stuff, put them within quotes for example googling for "C #ifdef compare" gives me as second hit this The C Preprocessor - GCC, the GNU Compiler Collection - GNU.org which has plenty of good info