2017-05-16 02:42 PM
Hello,
In STVD (+Cosmic) C compiler settings I choose *Display Errors & Warnings*.
I wrote simple code:
uint8_t a=0x01;
uint8_t b=0x10;
void test (uint8_t licz) {
}
void main (void) {
test (a & 0x01);
test (a | b);
test (a + b);
}
After compilation there are warnings:
#warning cpstm8 main.c:122(9) truncating assignment
#warning cpstm8 main.c:123(9) truncating assignment
#warning cpstm8 main.c:124(10) truncating assignment
To avoid warnings I need to use casts, for example:
test ((uint8_t)(a & 0x01));
The result of (a + b) operation do not exceed max value 8-bit long data.
Why are there warnings?
All data are declared as 8-bits long.
If the data will be declared as integer 16-bits long there’s no need to use casts.
2017-05-16 03:27 PM
>>The result of (a + b) operation do not exceed max value 8-bit long data.
Yeah in your specific case, it is not optimizing/warning about that case or with your constants, it is complaining about the general case where the values change and 240 + 230 doesn't fit.
2017-05-16 05:53 PM
I understand that, but why are there warnings in case of
test (a | b);
and
test (a & 0x01);
?
These expressions will never exceed max value 8-bit long data.
Moreover, if there will be all 16-bit data, and value for example
uint16_t a=0x0001;
uint16_t b=0x1000;The same logic and arithmetic operations don’t generate any warnings.
2017-05-18 02:37 AM
that's because the C language requires a lot of implicit conversions to int: if you write
test ((unsigned char)(a & 0x01));
test ((unsigned char)(a | b));the warnings will go away.
If a and b are already 16 bits the implicit conversion to int (that is 16 bits on the stm8) does not change anything, hence no warning.
Regards,
Luca