Before logging an issue, please update to the latest release of Visual Micro from the Downloads Page.

When Logging a Support Issue in the Forum, please ensure you have also:-

  • Enabled vMicro > Compiler > Show Build Properties
  • Re-Compile your program with these settings enabled
 
Save the new Output to a Text File and....
  • Click the Reply button and attach as .txt file OR
  • Click here to Email us with the file attached, and a link to your post
Support requests without the output above may be impossible to answer, so please help us to help you
 
Page Index Toggle Pages: 1 Send TopicPrint
Hot Topic (More than 8 Replies) Unexpected results from #if test (Read 5906 times)
Bob Jones
Full Member
***
Offline


Posts: 210
Location: Bellingham, WA
Joined: Dec 4th, 2015
Unexpected results from #if test
Jul 10th, 2016 at 11:35pm
Print Post  
What results would you expect from this test:
enum days {SUN, MON, TUE, WED, THU, FRI, SAT};

days day = TUE;

void setup()
{
#if day == SUN
     Serial.println("SUN");
#elif day == MON
     Serial.println("MON")
#elif day == TUE
     Serial.println("TUE")
#elif day == WED
     Serial.println("WED")
#elif day == THU
     Serial.println("THU")
#elif day == FRI
     Serial.println("FRI")
#elif day == SAT
     Serial.println("SAT")
#endif
}

void loop(){}

I expect to see TUE, but it prints SUN instead. This is proof of a pattern that I don't understand. The "#if" will always select the first value rather than the matching value if the value is based on an enum. 

Is this a bug or expected behavior? If it is expected, then how do I get the results I am looking for (i.e., how can I make the compiler select the correct choice to generate?
  
Back to top
 
IP Logged
 
Bob Jones
Full Member
***
Offline


Posts: 210
Location: Bellingham, WA
Joined: Dec 4th, 2015
Re: Unexpected results from #if test
Reply #1 - Jul 11th, 2016 at 12:06am
Print Post  
I did some more research and created an even simpler example that still doesn't work:

#define SUN 1
#define MON 2
#define TUE 3
#define WED 4
#define THU 5
#define FRI 6
#define SAT 7

int day = WED;

void setup()
{
     Serial.print("day=");
     Serial.println(day);  // PRINTS "4"

#if day == SUN
     Serial.println("SUN");
#elif day == MON
     Serial.println("MON")
#elif day == TUE
     Serial.println("TUE")
#elif day == WED 
     Serial.println("WED")
#elif day == THU
     Serial.println("THU")
#elif day == FRI
     Serial.println("FRI")
#elif day == SAT
     Serial.println("SAT")
#endif
}

// NOTHING PRINTS!!! 

day is an int with the value 4. WED is an int with the value 4. It should print WED but doesn't match any of the values. WHY?

  
Back to top
 
IP Logged
 
Bob Jones
Full Member
***
Offline


Posts: 210
Location: Bellingham, WA
Joined: Dec 4th, 2015
Re: Unexpected results from #if test
Reply #2 - Jul 11th, 2016 at 12:46am
Print Post  
Tim,

I am almost dead stop in the water until I can determine how the compiler directives are supposed to work. I get that they don't work with enum, but my last experimented proved that the #if..#elif..#elif sequence doesn't match anything. 

Since my code already blows memory in an UNO, I have to be able to conditionally include only the code needed by this specific robot, but right now I don't seem to have the tools to do this. 

Any thoughts on this?

Bob
Lips Sealed
  
Back to top
 
IP Logged
 
Bob Jones
Full Member
***
Offline


Posts: 210
Location: Bellingham, WA
Joined: Dec 4th, 2015
Re: Unexpected results from #if test
Reply #3 - Jul 11th, 2016 at 12:52am
Print Post  
Here is an even more basic example of the problem:

int SUN = 1;
int MON = 2;
int TUE = 3;
int WED = 4;
int THU = 5;
int FRI = 6;
int SAT = 7;

int day = WED;

void setup()
{
     Serial.print("day=");
     Serial.println(day);

#if day == SUN
     Serial.println("SUN");
#elif day == MON
     Serial.println("MON")
#elif day == TUE
     Serial.println("TUE")
#elif day == WED 
     Serial.println("WED")
#elif day == THU
     Serial.println("THU")
#elif day == FRI
     Serial.println("FRI")
#elif day == SAT
     Serial.println("SAT")
#endif
}

Output:
day=4
SUN
  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12188
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Unexpected results from #if test
Reply #4 - Jul 11th, 2016 at 5:59am
Print Post  
Hi bb,

Generic Arduino code questions are best directed to the Arduino forum. The Arduino ide works the same way.

Thanks
  
Back to top
IP Logged
 
Jo Sto
Ex Member
*


Re: Unexpected results from #if test
Reply #5 - Jul 11th, 2016 at 6:52am
Print Post  
#if, #elif are preprocessor directives, only used at compiletime, knowing nothing about c++ constants and variables.
If you would use if instead #if, elseif instead #elif i think the code would work as expected
« Last Edit: Jul 11th, 2016 at 2:07pm by »  
Back to top
 
IP Logged
 
Bob Jones
Full Member
***
Offline


Posts: 210
Location: Bellingham, WA
Joined: Dec 4th, 2015
Re: Unexpected results from #if test
Reply #6 - Jul 11th, 2016 at 3:00pm
Print Post  
Tim,

I think you are missing my point. My need is to know @ compile time which classes to include in the program. If I have five different motor drivers, I only need one in the resulting program, so I am absolutely dependent on #if and #elif... and they do not work as expected. 

I can send you multiple examples that prove this. I will also post this on the Arduino forum, but I know I can count on you for good and quick feedback. 

Please take a look at the code I just sent you and you will quickly see what I mean. 

Thanks,

Bob
  
Back to top
 
IP Logged
 
Bob Jones
Full Member
***
Offline


Posts: 210
Location: Bellingham, WA
Joined: Dec 4th, 2015
Re: Unexpected results from #if test
Reply #7 - Jul 11th, 2016 at 6:12pm
Print Post  
I posted this question on the Arduino forum: https://forum.arduino.cc/index.php?topic=412097.0 with slightly refined tests. 
  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12188
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Unexpected results from #if test
Reply #8 - Jul 11th, 2016 at 6:28pm
Print Post  
Cool.


This works and and prints "y"

Code (C++)
Select All
#define x  1
#define y  2
#define state y

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

void loop()
{
#if state==x
	Serial.println("x");
#elif state==y
	Serial.println("y");
#else
	Serial.println("Unkown");
#endif


} 

  
Back to top
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint