Defining and using types only when a macro is set, e.g. for debugging

I'd like to know if it is possible to define a type when a macro is defined with a particular value, but used later on in the code than the loop that contains the definition, e.g.

#define DEBUG 1

if ( DEBUG )
{

You need to be a bit more specific. The snippet you have shown is perfectly fine and will cause no issues, but has nothing to do with "Types".

Duplicate of http://forum.arduino.cc/index.php?topic=454002.0

Sorry, I tried to use a tab when I was writing the post and it posted the message before it was finished.

I meant something like this...

#define DEBUG 1

if ( DEBUG )
{
 uint8_t i = 8;
}

.
.
.
if ( DEBUG )
{
 j[i] = 5;
}

Again, it is hard to tell from a toy example. Your i variable will only live inside the brackets and be invisible outside them.

Usually you use #ifdef for something like this.

#ifdef DEBUG
    ...whatever you want to do...
#endif

ifdef is a preprocessor command that tests is the symbol next to it is defined or not. If it is not defined, everything between it and the matching endif is vanished from the code. As far as the compiler is concerned, it doesn’t exist.

The opposite, #ifndef, evaluates as true when the symbol is NOT defined.

There’s another fun thing you can do with conditionally defined macros.

#ifdef DEBUG
    #define DEBUG_PRINT(x) Serial.print(x)
    #define DEBUG_PRINTLN(x) Serial.println(x)
#else
    #define DEBUG_PRINT(x)
    #define DEBUG_PRINTLN(x)
#endif

If DEBUG is #defined, DEBUG_PRINTLN(“Hello world!”) will be expanded to Serial.println(“Hello world!”), but if it’s not defined it expands to nothing. It’s eaten by the memory hole.

Awesome, thanks.