0

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: "));
Serial.println(pinname);
#end

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

asked Apr 22, 2017 at 19:28

2 Answers 2

1

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":

#ifndef MY_VARIABLES_H
#def MY_VARIABLES_H
int counter;
bool flag;
#endif

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.

answered Apr 22, 2017 at 20:03
1

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).

answered Apr 22, 2017 at 20:47
1
  • 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. Commented Apr 22, 2017 at 22:27

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.