|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
|||||
|
|||||
|
What the...? casting bug?
'splain this, somebody:
Code:
#define STRAIGHT_DRIVE_DIRECTION ( 127 + 30 )
static int steeringValue = 0;
. . .
steeringValue = (int) ((STRAIGHT_DRIVE_DIRECTION));
printf( "nav: DTC %3d ", steeringValue );
Changing the assignment to: Code:
steeringValue = (int) ((unsigned char) (STRAIGHT_DRIVE_DIRECTION)); Is this weird, or is there something about type casting I don't understand? |
|
#2
|
|||||
|
|||||
|
Re: What the...? casting bug?
Ohhhhhhh!
Funny how you understand something as soon as you post it. The compiler takes (127 + 30) and performs signed 8-bit arithmetic, yielding -99. Casting as a signed (int) duly kept the sign: -99. Casting as an unsigned char reinterpreted the value as 157, which was then cast as a signed int: 157. My error was in assuming that constants were treated as unsigned chars, but they are treated as signed chars. |
|
#3
|
||||
|
||||
|
Re: What the...? casting bug?
Quote:
![]() |
|
#4
|
|||||
|
|||||
|
Re: What the...? casting bug?
Boy, you guys are fast.
Norm, I think if you had used Code:
#define STRAIGHT_DRIVE_DIRECTION ( 127u + 30 ) |
|
#5
|
|||||
|
|||||
|
Re: What the...? casting bug?
There's a compuler option for that. Project>Build Options...>Project. Go to MPLAB C18. check "Treat 'char' as unsigned" (under Category General). The command-line flag is '-k'
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| printf bug - beware! | WizardOfAz | Programming | 8 | 10-03-2004 09:50 |
| bug? | Duke 13370 | CD Forum Support | 4 | 30-12-2003 21:44 |
| Gingery Lathe - Metal Sand Casting | sanddrag | General Forum | 13 | 14-08-2003 21:41 |
| Last Call for bug reports | rbayer | Programming | 0 | 08-01-2003 02:48 |
| Small bug | Jay Lundy | CD Forum Support | 1 | 18-06-2001 17:03 |