开发者

Conditional operator: conversion from 'int ' to 'unsigned char ', possible loss of data

开发者 https://www.devze.com 2023-03-08 13:57 出处:网络
I have the following C code: a = (b == B1) ? A1 : A2; a, b, A1, A2 and B1 are all of type unsigned char. A1, A2 and B1 are all const开发者_Python百科ants.

I have the following C code:

a = (b == B1) ? A1 : A2;

a, b, A1, A2 and B1 are all of type unsigned char. A1, A2 and B1 are all const开发者_Python百科ants.

When compiling under VC++ I see the following warning:

warning C4244: '=' : conversion from 'int ' to 'unsigned char ', possible loss of data

I don't understand this warning - none of the variables are of type int. Presumably some kind of implicit conversion is happening, but why?

Strangely, the following code (which is functionally identical) compiles with no warnings:

if (b == B1) {
  a = A1;
} else {
  a = A2;
}

So far as I'm aware, the two code extracts should be identical.


In C language arithmetic arguments of ternary ?: operator are subjected to usual arithmetic conversions, i.e. they are promoted to int before any further computations are performed. It doesn't really matter whether they are constants or not. What matters is that they have type unsigned char (as you said) and unsigned char under ?: is always promoted first. Basically, C language never performs any computations in types smaller than int. Everything smaller is converted to int first.

This is what happens in your case as well. Basically, your

a = (b == B1) ? A1 : A2;

is interpreted by C language as

a = ((int) b == (int) B1) ? (int) A1 : (int) A2;

and this is why you get the warning. Again, the fact that A1 and A2 are constants plays no role whatsoever.

The

a = A1;

does not subject the right-hand side to integral promotion, which is why there's no warning here. Moreover, in this trivial case (direct assignment) even if A1 was explicitly declared as an int constant, most compilers would not issue a warning if they could see that the constant is in range of the target type unsigned char. The case with ?: is more complicated, so the compilers might revert to the generic behavior and issue a warning.


Although functionally the same, both codes are not quite equals.

You see, when you use a ternary operator, that is the condition ? true_value : false_value, the compiler tries to infer between the values the best possible type.

Because A1 and A2 are constant (as you state in the O.P.) the compiler replaces their position by their actual values, ignoring completely the data type, rendering both as integers.

Hence the need to cast the result, as such:

a = (unsigned char)((b == B1) ? A1 : A2);


  a = (unsigned char)((b == B1) ? A1 : A2);

I believe this is because there is no rvalues in the sub statements -- they get auto converted to int in the lexer. The code above should solve the warning message and have no effect on the resulting code.

0

精彩评论

暂无评论...
验证码 换一张
取 消