Pitfalls
The bytes remains the same despite copying, so you can get too funky:
int v = -12345;
unsigned int uv = v;
printf("v = %d, uv = %d\n", v, uv);
This prints “v = -12345, uv=4294954951”. As in: when you copy rvalues, the bit pattern gets copied and not the numerical number itself; so, it will overflow.
You can use U to force an signed quantity to be unsigned:
unsigned int uv = -12345U;
sign promotion
If you have the nerve of putting a comparing things of different types (don’t), then, the signed quantities gets promoted to be unsigned.
That is, we get that:
- -1 < 0U is false because the -1 is promoted to an unsigned integer
- 2….7 > -2….7 is true because nothing is converted
type size promotion
If you have the nerve of putting a comparing things of different types (don’t), then, the smaller types get promoted to being a bigger types.
- casting from small unsigned value to larger unsigned value just requires us prepending a buncha zeros as needed
- casting from a small signed value to larger signed value requires us repeating the left most value to fill out the rest of the variable (-1 = 11111, so bigger -1 = 11111 (repeated) 111)
- lasting from a large value to a smaller value will cause truncation
type size truncation
Take, for instance:
int x = 53191;
short sx = x;
int y = sx;
The short is 2 byte, which means that 2 of the left bytes of the int got dropped.