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

[Solved]Comparison with a negative Integer.

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



Joined: 27 Jul 2013
Posts: 79

View user's profile Send private message

[Solved]Comparison with a negative Integer.
PostPosted: Sat Mar 22, 2014 9:26 am     Reply with quote

Compiler Version 5.008

Compiler isn't comparing negative integers correctly. LCD confirms negative int received by the function, but the code in IF is executed every time.

Is this a bug, or am I missing something.

Code Below:

Code:

void right(int percent){
   printf(lcd_putc,"\f%d",percent);
   if((int)percent>=0){
      printf(lcd_putc,"\n%d,if",percent);
      output_high(RightC1);
      output_low(RightC2);

   }else{
      percent = -percent;
      output_high(RightC2);
      output_low(RightC1);
      printf(lcd_putc,"\n%d,else",percent);
   }
   pwm2(percent);
}


Last edited by haxan7 on Sat Mar 22, 2014 10:11 am; edited 2 times in total
temtronic



Joined: 01 Jul 2010
Posts: 9226
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Mar 22, 2014 9:30 am     Reply with quote

could be either as 5.008 is a real early however my understanding is that 'int' hmm...defaults to unsigned integer ( 0-255).
The observation that the LCD displays the value correctly just shows the printf functions correctly.

hth
jay
haxan7



Joined: 27 Jul 2013
Posts: 79

View user's profile Send private message

PostPosted: Sat Mar 22, 2014 9:42 am     Reply with quote

temtronic wrote:
could be either as 5.008 is a real early however my understanding is that 'int' hmm...defaults to unsigned integer ( 0-255).
The observation that the LCD displays the value correctly just shows the printf functions correctly.

hth
jay


Any way of fixing this without changing the compiler?
temtronic



Joined: 01 Jul 2010
Posts: 9226
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Mar 22, 2014 9:50 am     Reply with quote

hmm. the obvious one is to declare the variable as a signed integer.

If you press F11 while your project is open the CCS HELP manual magically appears, goto contents,goto data definitions,goto basic types....


hth
jay
haxan7



Joined: 27 Jul 2013
Posts: 79

View user's profile Send private message

PostPosted: Sat Mar 22, 2014 9:52 am     Reply with quote

Definitely a bug. int should default to signed int by C standard.

using signed int percent instead of int percent fixed the problem.
dyeatman



Joined: 06 Sep 2003
Posts: 1934
Location: Norman, OK

View user's profile Send private message

PostPosted: Sat Mar 22, 2014 10:32 am     Reply with quote

It is NOT a bug just a choice by CCS.
CCS clearly states in the manual that the INT8 defaults to UNSIGNED.
_________________
Google and Forum Search are some of your best tools!!!!
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