unwanted compiler warnings

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

unwanted compiler warnings

1,909 次查看
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
 
标签 (1)
标记 (1)
0 项奖励
回复
2 回复数

765 次查看
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 项奖励
回复

765 次查看
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 项奖励
回复