uint32_t a = 10;
uint16_t b = 0;
if (a > b)
{
printf("a > b\n");
}
I was expecting that b
would get promoted to int
and the compiler would complain about comparing signed and unsigned.
But the compiler did not raise any warning.
When I change
if (a > b)
to if (a > (b * 10))
the compiler says
warning: comparison between signed and unsigned
Why is this so?
And is it safe to typecast this to uint32_t?
if (a > (uint32_t)(b * 10))
Does uint16_t gets promoted to int?
If int
is 32 bits, sure. If not, then it isn't promoted.
I was expecting that b would get promoted to int
It is, on a 32 bit system.
But the compiler did not raise any warning.
It's not really the compilers job to keep track of that. If you get a warning, it's just a bonus. But also please note that in your example the conversion is done in two internal steps: first the integer promotions is performed on the uint16_t
making it int
. But then immediately afterwards, the usual arithmetic conversions convert that int
to a uint32_t
. So essentially as far as the programmer is concerned, the uint16_t
is converted to uint32_t
and that conversion is always safe.
Generally, operations mixing unsigned operands of different sizes are safe.
if (a > (uint32_t)(b * 10))
b
gets promoted to int
and 10
is already int
. There's no way for the multiplication to overflow since it can't get larger than 655350. You can cast to uint32_t
afterwards if you like, but it doesn't rally achieve anything except being explicit.
More rugged code would have done the cast before the multiplication or perhaps used 10u
instead. But in this specific case it really don't matter.