Whilst using #if/#end constructs, how can I determine whether a specific variable has already been declared?, e.g.,

#if [the variable "pinname" has been declared]
Serial.print(F("Pin name: "));

If this question has been addressed, I cannot find a reference for it.

2 Answers 2


Normally you use the "#ifdef" directive.

The opposite is "#ifndef".

For example, if you only wanted to include variable definitions once many use this pattern for a header file named, say, "my_variables.h":


int counter;
bool flag;


In this way, no matter how many times you include this file, you only define the variables once. Because after the 1st time the compiler sees this file, the variable MY_VARIABLES_H is defined and the subsequent time the compiler sees this file, the compiler will skip over the variable defines.


You cannot. Simple as that.

I suspect this is an XY Problem. If you edit your post stating what you're actually trying to do, I'll edit my answer (or delete it if I don't know).

  • my example is exactly what I am trying to do. @Mark Smith, In this case, specific variables and names are defined based on board attributes. Another routine is attempting to display such, but obviously only for defined variables. Of course, I can handle this with the necessary book-keeping. However, I'd like to know if there is a simpler solution. If not, I know exactly how to get the results i want.
    – KC Tucker
    Apr 22, 2017 at 22:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.