There are many reasons why this warning may be produced when compiling with MPLAB® XC8. Here are a few examples that might help you understand why it occurs in your code.
The most common code to trigger this warning is assignments to signed data types. For example:
As a signed 8-bit quantity, c can only be assigned values -128 to 127. The constant is equal to 255 and is outside this range.
In general, if you want a value that has all bits set, and you don't want to be specific about how many bits that is, write the code as e.g.
This will set all the bits of c.
This code will emit the warning of "arithmetic overflow" when BEEP_TIME is assigned to bep.
This is because 8000000 is by default a long number, but 3200 and 64 aren't, so 64*3200 is evaluated to int length, and overflows. Use an 'L' suffix to force long arithmetic in the expression above as:
Note the 'L' appended to 64 - this makes it a long number, and the
evaluation of 64L*3200 will be done to a 32-bit length.
The statement ‘i == (sizeof(vec) - 1)’ will emit the warning “arithmetic overflow in constant expression”.
This is because the compiler treats the value "- 1" as "+ 0xFFFF" (with a 16-bit operand). The result of the sizeof operator is 5. So the 5 is added to 0xFFFF which causes an overflow; and this is what the compiler warns about.
To avert the warning re-write the code as:
The x will be trated as 1, and not 0xFFFF, because it has been declared as unsigned type.