View Single Post
  #2   Spotlight this post!  
Unread 20-05-2006, 01:00
aaronm_k aaronm_k is offline
Registered User
no team
 
Join Date: Jan 2006
Rookie Year: 2006
Location:  
Posts: 10
aaronm_k is on a distinguished road
Post Re: Stupidest Programming Mistakes

Quote:
Originally Posted by Eldarion
One of the things that keeps tripping us up is that the MPLAB compiler does not follow standard C data type promotion rules.

For example, given the statement below, standard C would promote both data types to integer, then multiply. The MPLAB compiler, however, multiplies a and b as unsigned chars and then sticks the result in the integer value, resulting in an overflow where you least expect it!

Code:
unsigned char a = 127:
unsigned char b = 127;
int result;

result = (a * b);
You can turn on integer promotions by going to "Project" > "Build Options" > "Project" in MPLAB, clicking the "MPLAB C18" tab, and adding "-Oi " to the beginning of the "Use Alternate Settings" text box. (Checking the "Enable integer promotions" check box will NOT work.) Believe me, I had plenty of trouble with this too!