unwanted compiler warnings

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

unwanted compiler warnings

1,593 Views
Remix
Contributor I
In the Code Warrior for ColdFire compiler ( version 5.9.0, processor CF 5235) , if I write a function like this :
 
Code:
void Operation(){ char a, b,c; a = b+c;}

 
I get a warning of "implicit arithmetic conversion from int to char"
This is very annoyng, while I think it is important to still have this warning ifI write
 
Code:
void Operation(){ char a; int b,c; a = b+c;  //here I try to fit an integer in a char without typecasting}

 
is there a way to avoid the warning in the first case , without disabling the "implicit conversion" warning?
 
Thank you
 
Stefano
 
Labels (1)
0 Kudos
2 Replies

449 Views
Arev
Contributor III
Hello,
 
The max value of a signed char is 127, so the result of adding two char must be stored in a larger variable to accept (127 + 127) = 254.
 
The Warning prevent you from this error. 

Bye
0 Kudos

449 Views
sjmelnikoff
Contributor III
If you are abolutely sure that 'a' is big enough to contain the result of the addition, you can do a cast:

a = (u8) (b + c)

But as Arev said, the compiler is quite right to warn you about this, as if the result is sometimes bigger than 127, this could be a difficult bug to catch!

Steve.
0 Kudos