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

Why this code suxx?

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



Joined: 07 Sep 2003
Posts: 32

View user's profile Send private message

Why this code suxx?
PostPosted: Fri Mar 05, 2004 3:44 pm     Reply with quote

Hai,

I'm using this code in my counter project.It is simple but it does not work correctly.It only increase variable i when user press button 1.Then multiplies i by 10, write to lcd.But above 250, lcd shows 4 instead of 260.
Why?
.
.
...
if (cn == 2)
{
i++;
value = i*10;
sprintf(c, "\%lu", value);
lcd_clear();
lcd_gotoxy( 1,1 );
lcd_string(c);
delay_ms(200);
}
..
.
.
Ttelmah
Guest







Re: Why this code suxx?
PostPosted: Fri Mar 05, 2004 3:52 pm     Reply with quote

Analyzer wrote:
Hai,

I'm using this code in my counter project.It is simple but it does not work correctly.It only increase variable i when user press button 1.Then multiplies i by 10, write to lcd.But above 250, lcd shows 4 instead of 260.
Why?
.
.
...
if (cn == 2)
{
i++;
value = i*10;
sprintf(c, "\%lu", value);
lcd_clear();
lcd_gotoxy( 1,1 );
lcd_string(c);
delay_ms(200);
}
..
.
.

Because you are performing the arithmetic on an integer, which can only hold 0-255. (26*10) mod 256, is 4.
If you want the arithmetic to use a 'long', either cast 'i' to a long, or use a long constant for the multiplication. Hence:
value = i*10l;

or

value = (long)i*10;

will force the arithmetic to use the long multiply instead.

Best Wishes
Analyzer



Joined: 07 Sep 2003
Posts: 32

View user's profile Send private message

PostPosted: Fri Mar 05, 2004 4:32 pm     Reply with quote

Thank you very much.I learnt something about c.
Best Wishes.

Analyzer.
Darren Rook



Joined: 06 Sep 2003
Posts: 287
Location: Milwaukee, WI

View user's profile Send private message Send e-mail

PostPosted: Fri Mar 05, 2004 4:56 pm     Reply with quote

Analyzer wrote:
Thank you very much.I learnt something about c.


Be aware that the CCS C compiler's int is 8 bits, but in other compilers it is usually signed 16 bits. In fact, with each new compiler you should look at bit sizes of data types.

Just wanted to let you know since you're learning C, and CCS does it a little differently than other C compilers.
Ttelmah
Guest







PostPosted: Sat Mar 06, 2004 3:38 am     Reply with quote

Darren Rook wrote:
Analyzer wrote:
Thank you very much.I learnt something about c.


Be aware that the CCS C compiler's int is 8 bits, but in other compilers it is usually signed 16 bits. In fact, with each new compiler you should look at bit sizes of data types.

Just wanted to let you know since you're learning C, and CCS does it a little differently than other C compilers.

Yes. This is one of the areas, where CCS, keeps to the original K&R C defintions, rather than the latter ANSI versions. In K&R, they warn specifically, that an 'int', will be the native size for the processor involved, and then give examples showing how on a early PDP mini-computer, using a 12bit integer, this will result in this type of problem. In ANSI, the 'native' size was allowed to slip, and the default integer size is made 16bits. The advantage of the latter, is portability, but the former has the advantage of ensuring that the faster type will be used for simple operations (a good idea on a small 'embedded' processor), but brings with it the caveat that you must 'think' about the sizes involved...

Best Wishes
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