The type of an expression is determined at compile time, it can't depend on runtime conditions. When the two resulting expressions in a conditional (aka tertiary) expression are different, they have to be converted to a common type, and this is the type of the expression as a whole. See Return type of '?:' (ternary conditional operator) for the details.
In your case, a
is presumably int
, and (char)('A' + a - 10)
is char
, so the common type is int
, so cout
uses its method for printing int
rather than char
.
Instead of a tertiary, use an ordinary if
:
if (a > 9) {
cout << static_cast<char>('A' + a - 10);
} else {
cout << a;
}
Or cast to char
after doing the conditional.
cout << static_cast<char>(a > 9 ? ('A' + a - 10) : ('0' + a));