View previous topic :: View next topic |
Author |
Message |
dcm684
Joined: 08 Sep 2010 Posts: 8
|
#if defined() Usage |
Posted: Wed Apr 09, 2014 9:02 am |
|
|
I have the following block of code in my program
Code: | #if (defined(ANIMAL) && (ANIMAL == COW))
/* Do stuff here */
#endif
|
The compiler throws the warning "Undefined identifier ANIMAL."
Does the compiler evaluate both items in the comparison even if the first item, defined(ANIMAL), is false? Apart from nesting the second evaluation in the first, is there a way to prevent this?
Thanks
Compiler Version: 5.020 PCH |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19537
|
|
Posted: Wed Apr 09, 2014 11:17 am |
|
|
What compiler version?.
It'll have to evaluate the statement, but the ANIMAL==COW, should still be accepted.
It works OK, on current compilers, and even on back to 4.118.
Are you sure you are typing both '&' symbols?. Remember a single & will involve a bitwise combination, while the double gives a logical and. The former will require ANIMAL and COW to be defined.
Best Wishes |
|
|
dcm684
Joined: 08 Sep 2010 Posts: 8
|
|
Posted: Wed Apr 09, 2014 11:38 am |
|
|
Actually, "ANIMAL" is not defined. Its a value that I periodically #define for certain production environments. I'm sorry that I did not state that earlier.
I copied and pasted my code straight from MPLAB X.
My compiler version is 5.020 PCH.
What I expect would happen is that the compiler would see "defined(ANIMAL)" realize that "ANIMAL" is not defined and as a result realize the statement will never be true so it would skip the "ANIMAL == COW" comparison.
I would not expect any warning to be thrown since there is a check to see if "ANIMAL" was defined before trying to use the not defined "ANIMAL". |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19537
|
|
Posted: Wed Apr 09, 2014 1:14 pm |
|
|
Seriously, the statement, works as posted in every compiler I have tried. With both values undefined.
Are you sure there isn't a fault in an earlier line?.
There is a classical problem with CCS, where it'll accept things that are wrong, and then error a lot of lines later, when something reaches a 'limit' internally, so makes it realise something is wrong. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19537
|
|
Posted: Wed Apr 09, 2014 2:10 pm |
|
|
Also worth pointing out, that warnings, are only warnings. They don't stop the compiler compiling. If you don't want the warning to display, Unidentified identifier is 215, so
#IGNORE_WARNINGS 215
would stop it displaying.
However I have to repeat that this code compiles correctly, without this warning. If you define 'ANIMAL, then it'll give:
">>> Warning 215 location: Undefined identifier COW"
However without this defined, it compiles without warnings, so something earlier is wrong.... |
|
|
|