The following code example demonstrates that the implicit cast from short to char fires at level 3, while the implicit cast from int to char fires only at warning level 4.
int main()
{
short as = 1;
int ai = 1;
char b1 = as; // warning C4244 (Level 3)
char b2 = ai; // warning C4244 (Level 4)
return 0;
}
What’s the reason for this – the documentation omits the reason?
I ran into this issue after changing the type of a variable and using this warning for identifying possible conversion problems. I missed the warnings and recognized that I had to switch to level 4.
One reason could be that arithmetic operations involving smaller types are actually performed with the values promoted to ints, so it is slightly more reasonable to assign an int result back to the original size.
Assigning a short to a char is almost always a mistake.