CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

#if defined() Usage

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
dcm684



Joined: 08 Sep 2010
Posts: 8

View user's profile Send private message

#if defined() Usage
PostPosted: Wed Apr 09, 2014 9:02 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Apr 09, 2014 11:17 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Apr 09, 2014 11:38 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Apr 09, 2014 1:14 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Apr 09, 2014 2:10 pm     Reply with quote

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....
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group