Fun with C
Much as I enjoy C, there are times I'm reminded of delightful traps.
Say you've got this code:
uint16_t a,b;
uint32_t result;
result = a * b;
No problem, right? No warnings, nothing.
Not quite. =:)
It's that arithmetic operator. If it sees any args less than the bit width of int, they get automatically promoted to ints - not unsigned ints, just plain signed ints. So, you wind up with a warning that conversion to uint32_t from int may change the sign of the result.
What you want to do in that case is preempt the compiler by typecasting both a and b to uint32_t; being (assuming an int width of 32 bits!) 32 bits, same as int, those signed promotions don't take place,
Fun. ^_^
re: Fun with C
@porsupah Warning: Implicit conversion of unsigned ints may result in your probe crashing into mars.
Awoo.space is a Mastodon instance where members can rely on a team of moderators to help resolve conflict, and limits federation with other instances using a specific access list to minimize abuse.
While mature content is allowed here, we strongly believe in being able to choose to engage with content on your own terms, so please make sure to put mature and potentially sensitive content behind the CW feature with enough description that people know what it's about.
Before signing up, please read our community guidelines. While it's a very broad swath of topics it covers, please do your best! We believe that as long as you're putting forth genuine effort to limit harm you might cause – even if you haven't read the document – you'll be okay!