Type Conversions#
Implicit Type Conversions#
Bad news: C does not care much about widths and signs
- Assignment to narrower types simply cuts off 
- Sign propagation is undefined 
- Sign may change across signed/unsigned assignments 
- ⟶ History is full of integer overflow bugs, sign bugs etc. 
- GCC (and other compilers) has options that warn on possible type-bugs (can be very loud though) 
Rules are not easy to comprehend - especially the Why behind ⟶ Examples …
Sign Bugs#
Unsigned to signed, same width
| unsigned int ui = 4294967295U;
int i = ui;
 | 
 | 
The other way around: signed to unsigned
| int i = -1;
unsigned int ui = i;
 | 
 | 
Attention
This is desired behavior from the very beginning ⟶ no compiler error, no compiler warning!
Though at least GCC can be convinced to warn:
- -Wsign-conversion
- more global: - -Wconversion
Truncation#
| unsigned long ul = 4294967296U;
unsigned int ui = ul;
 | 
 | 
Note
-Wconversion
Sign Propagation#
| char c = '\310';
int ic = c;
 | 
 | 
Note
-Wconversion
Conversion Using Operators#
Hard rule
If an operator gets passed different types, then the “weaker” is converted to the “stronger” - the result is of the “stronger” type.
What does that mean? (disregarding unsigned)
- If one operand is - long double, then the other is converted
- else, if one is - double, …
- else, if one is - float, …
- else, - charand- shortare converted to- int
- ⟶ - intis the default type for arithmetic operations
Conversion and unsigned (1)#
Hard rule. There is no hard rule. Well almost:
When mixing
unsignedandsignedintegers of the same width, thensignedis converted tounsigned
Warning
Gosh!
Additionally: widths are hardware defined!
| -1L < 1U
 | 
 | 
| -1L < 1UL
 | 
 | 
Warning
This is desired behavior from the very beginning ⟶ no compiler error, no compiler warning!
Though at least GCC can be convinced to warn:
- -Wsign-conversion
- more global: - -Wconversion
Conversion and unsigned (2)#
Beware of mixing!
- Not a problem if the - signedpart can never become negative
- Big problem otherwise! 
int x;
unsigned int y;
if (x < y) ...
$ gcc -Wsign-compare ...
warning: comparison between signed and unsigned integer expressions
Compiler Warnings#
All that is desired behavior!
- Read: historical baggage 
- ⟶ compiler warnings have to be explicitly enabled 
| Option | Meaning | 
|---|---|
| 
 | Sign could change | 
| 
 | Value and sign … | 
| 
 | Comparison with mixed signed value | 
| 
 | E.g.  | 
| 
 | Selection of “good” warnings | 
| 
 | … more good warnings | 
| 
 | Does not hurt | 
| 
 | Anti-Sloppiness: warnings become errors | 
Tip
General advice: the more the better!
Last Warning#
C’s datatypes are immensely hazardous. More hazardous is, though:
- Overengineering 
- Messy design 
- Loosing control over one’s data structures 
- Not knowing ranges of variables 
- Not being open to program modification 
Forced Conversion - Cast#
Should an automatic conversion be identified as being wrong (e.g. because the compiler warns), it can be overridden …
int x;
unsigned int y;
if (x < (signed)y) ...
