Hi, I have this code:
void main(void) { data unsigned char a = 0; data unsigned char b = 0; data unsigned int c = 1000;
b = c;
while(1) { a+= 1; } }
And it compiles with no errors or warnings. Why is this? Why does it not raise an error because the 16-bit value c is being stored in the 8-bit value b? The Intpromote is turned off, so that is not it.
Yours, confused.
Robbie Martin.
Because there is no requirement for it to do so!
See: http://www.keil.com/forum/docs/thread11903.asp If you want a strongly-typed "nanny" of a language that requires the compiler to hold your hand at all times, and keep all sharp objects out of your reach - then 'C' is not the one for you!
The C language allows automatic conversion between different integer widths for programmer convenience. If the language didn't convert, you'd have to cast in every expression with different sorts of integers. This feature is also, as you note, occasionally a programmer inconvenience as well.
PC-Lint, from Gimpel Software, will issue warnings for potential loss of precision in cases like this. Well worth the negligible price.
View all questions in Keil forum