|
|
View previous topic :: View next topic |
Author |
Message |
Analyzer
Joined: 07 Sep 2003 Posts: 32
|
Why this code suxx? |
Posted: Fri Mar 05, 2004 3:44 pm |
|
|
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? |
Posted: Fri Mar 05, 2004 3:52 pm |
|
|
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
|
|
Posted: Fri Mar 05, 2004 4:32 pm |
|
|
Thank you very much.I learnt something about c.
Best Wishes.
Analyzer. |
|
|
Darren Rook
Joined: 06 Sep 2003 Posts: 287 Location: Milwaukee, WI
|
|
Posted: Fri Mar 05, 2004 4:56 pm |
|
|
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
|
|
Posted: Sat Mar 06, 2004 3:38 am |
|
|
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 |
|
|
|
|
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
|