0
\$\begingroup\$

I am using MPLABX v2.25 and XC32 compiler v1.33 on a Mac.

Maybe I am not fluent with the use of the preprocessor, but the macro expansion window in MPLABX seems to produce rather unexpected results.

Example-1:

#define PIBO TEST  
void ftest(void)  
{  
#if (PIBO == TEST)  
  printf("Hello");  
#endif  
}  

Expected Result: printf() should appear in the macro expansion window
Result: printf() does appear in the macro expansion window

So far so good.

Example-2:

#define PIBO FOOTEST  
void ftest(void)  
{  
#if (PIBO == TEST)  
    printf("Hello");  
#endif  
}  

Expected Result: printf() should NOT appear in the macro expansion window
Result: printf() does appear in the macro expansion window

Example-3:

void ftest(void)  
{  
#if (0)  
    printf("Hello");  
#endif  
}  

Expected Result: printf() should not appear in the macro expansion window
Result: printf() does not appear in the macro expansion window

Is the MPLAB X macro expansion window showing faults or is it my code or understanding of how this is supposed to work?

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Not sure how the pre-processor would process it exactly but because == can't be used for string comparison in C I wouldn't expect it to work. The more usual thing would be to #define FOOTEST 1 and #define TEST 2 etc so the comparison is a numeric one. \$\endgroup\$
    – PeterJ
    Commented Feb 6, 2015 at 7:27
  • \$\begingroup\$ In C, undefined symbols in the preprocessor evaluate as 0. Therefore, your first two examples all evaluate as 0 == 0 which is true. if you #define x 1 and #define y 2 then (x == y) evaluates as (1 == 2) which is false. \$\endgroup\$
    – DoxyLover
    Commented Feb 6, 2015 at 7:28
  • \$\begingroup\$ @DoxyLover, I wasn't sure how it handled undefined symbols, you might as well post that as an answer. \$\endgroup\$
    – PeterJ
    Commented Feb 6, 2015 at 7:31

1 Answer 1

3
\$\begingroup\$

In C, undefined symbols in the preprocessor evaluate as 0. Therefore, your first two examples all evaluate as 0 == 0 which is true.

If you

#define x 1

then

#define y 2

then (x == y) evaluates as (1 == 2) which is false.

\$\endgroup\$

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.