View Single Post
  #2   Spotlight this post!  
Unread 03-03-2005, 19:46
Mark McLeod's Avatar
Mark McLeod Mark McLeod is offline
Just Itinerant
AKA: Hey dad...Father...MARK
FRC #0358 (Robotic Eagles)
Team Role: Engineer
 
Join Date: Mar 2003
Rookie Year: 2002
Location: Hauppauge, Long Island, NY
Posts: 8,801
Mark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond repute
Re: A strange occurence..

That programmer fell victim to a deliberate feature of the compiler.

Rather than do calculations at the ANSI specified int size, the PIC compiler deliberately tries to keep the sizes used in calculations as small as possible to save valuable space. It will do calculations at the largest type used in the calculation. If they are all char then the calculations will be done in char and the program will suffer from any overflows that might occur during the intermediate calculations. You can override the default and force the compiler to do promote all calculations to at least integer.



See section 2.7.1 of the MPLAB C18 C Compiler User’s Guide,

http://ww1.microchip.com/downloads/en/DeviceDoc/C18_UG__51288e.pdf



“2.7 ISO DIVERGENCES

2.7.1 Integer Promotions

ISO mandates that all arithmetic be performed at int precision or greater. By default,

MPLAB C18 will perform arithmetic at the size of the largest operand, even if both

operands are smaller than an int. The ISO mandated behavior can be instated via the

-Oi command-line option.

For example:

unsigned char a, b;

unsigned i;

a = b = 0x80;

i = a + b; /* ISO requires that i == 0x100, but in C18 i == 0 */

Note that this divergence also applies to constant literals. The chosen type for constant

literals is the first one from the appropriate group that can represent the value of the

constant without overflow.”
__________________
"Rationality is our distinguishing characteristic - it's what sets us apart from the beasts." - Aristotle