The value of f
and, in fact, the behaviour of the program, is implementation-defined.
In C++14 onwards1, for a signed
char
, and assuming that CHAR_MAX
is 127
, a
will probably be -5
. Formally speaking, if char
is signed
and the number does not fit into a char
, then the conversion is implementation-defined or an implementation-defined signal is raised.
b
is 251
.
For the comparison a == b
(and retaining the assumption that char
is a narrower type than an int
) both arguments are converted to int
, with -5
and 251
therefore retained.
And that's false
as the numbers are not equal.
Finally, note that on a platform where char
, short
, and int
are all the same size, the result of your code would be true
(and the ==
would be in unsigned
types)! The moral of the story: don't mix your types.
1 C++14 dropped 1's complement and signed magnitude signed char
.