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

Int32 comparison problem

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



Joined: 18 Aug 2010
Posts: 10

View user's profile Send private message MSN Messenger

Int32 comparison problem
PostPosted: Fri May 06, 2011 12:43 am     Reply with quote

Hi!

I have a the following code:
Code:

int32 AA,BB;
AA=-4;

if(AA > 0){
  // do some thing 1
}

BB=0:
if(AA > BB){
  // do some thing 2
}

Allways the code executes "do some thing 1" and "do some thing 2".

Why??

Thanks for your help
calvin



Joined: 18 Aug 2010
Posts: 10

View user's profile Send private message MSN Messenger

Another case:
PostPosted: Fri May 06, 2011 1:01 am     Reply with quote

For more illustration:
Code:

          if(-2 < -4){
            EVENTO(196);
          }
         
          if(-2 > -4){
            EVENTO(197);
          }

CCS generates the following .lst
Code:

....................           if(-2 < -4){
....................             EVENTO(196);
....................           }
....................           
....................           if(-2 > -4){
....................             EVENTO(197);
2156:  MOVLW  C5
2158:  MOVWF  x9C
215A:  MOVLB  0
215C:  CALL   0CE6
....................           }


why???
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Fri May 06, 2011 1:47 am     Reply with quote

With 8 Bit CCS compilers, int defaults to unsigned (unless overridden by a #TYPE statement).
So all negative constants are interpreted as positive values, the result is correct.
calvin



Joined: 18 Aug 2010
Posts: 10

View user's profile Send private message MSN Messenger

PostPosted: Fri May 06, 2011 7:02 am     Reply with quote

Thanks FvM I will read about that.But, is fine too that the compile doesnt generate code for some statements? that is not for signed or unsigned data types.
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Fri May 06, 2011 8:27 am     Reply with quote

If the compiler can tell at compile time that certain code can never be reached, it will not generate machine code for those statements. They are optimized out of existence.
_________________
The search for better is endless. Instead simply find very good and get the job done.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Fri May 06, 2011 8:36 am     Reply with quote

Quote:
But, is fine too that the compile doesnt generate code for some statements?

Sure. Conditional code after a constant compare that evaluates to false will be removed.
calvin



Joined: 18 Aug 2010
Posts: 10

View user's profile Send private message MSN Messenger

PostPosted: Fri May 06, 2011 11:54 am     Reply with quote

Thanks to all for your help.
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