Proven C Book←↑→

Appendix C — Implicit conversions in summary

This appendix compresses the rules of chapter 30 into tables and lists.

Integer promotion#

When bool, char, signed char, unsigned char, short, unsigned short or a bit-field takes part in arithmetic:

That is, arithmetic does not happen on a type narrower than int.

The usual arithmetic conversions (the common type of two operands)#

  1. If one side is long double → both long double
  2. Otherwise if one side is double → both double
  3. Otherwise if one side is float → both float
  4. If both are integers → after promoting each:

    • if the signedness is the same → the wider side
    • if the unsigned side is wider or equal → the unsigned side
    • if the signed side is wider and holds all of the other’s values → the signed side
    • otherwise → the unsigned version of the signed side’s type

The practical summary: when two types mix, the wider one wins; when the widths are equal, the unsigned side wins. Avoid comparisons of mixed signedness (turn the warnings on and the compiler points them out).

Default argument promotions in variadic calls#

Arguments crossing into a place with no type in the prototype (...):

So printf has no float-specific format, and %f takes a double (chapters 20, 26 and 67).

Integer conversion rank (the criterion of the conversion rules)#

The order used when the usual arithmetic conversions choose “the wider side”. Higher is further left.

long long  >  long  >  int  >  short  >  char / signed char / unsigned char  >  bool

When reading the rank order, keep three facts in mind: how the three character types rank, how bool ranks, and how rank relates to width.

① char, signed char and unsigned char have the same rank. Not because of width but because the standard says so (§6.3.1.1), the three sit in one slot.

② bool’s rank is lower than that of every other standard integer type. The standard’s sentence is “the rank of bool shall be less than the rank of all other standard integer types” (§6.3.1.1) — unsigned ones included, not only the signed.

③ Rank is not width. The ranks of a signed type and its unsigned counterpart (int and unsigned int) are the same, and then the unsigned side wins in the usual arithmetic conversions. That is why -1 < 1u is false — -1 turns into a huge unsigned value (chapter 30). Ranks can differ at the same width (long and long long on 64-bit Linux, or extended integer types an implementation provides), and at the same rank the signedness settles the result.

Which conversion brings which danger#

conversionwhat happensdanger
signed → unsignedthe two’s complement value is reinterpreted as it isa negative becomes a huge value
unsigned → signedas it is if it fits, otherwise implementation-definedthe value changes at the boundary
wide integer → narrow unsigned integerthe remainder modulo 2𝑁 (defined)quiet loss of value
wide integer → narrow signed integeras it is if it fits, otherwise implementation-definedresults can differ between implementations (still so in C23)
integer → realrounded to the nearest valueloss of precision for large integers (chapter 7)
real → integerthe fractional part is discarded (towards 0)outside the range it is outside the contract
double → floatthe precision is reducedrounding error
pointer → void *losslessonly the type information is lost
void * → another pointerlosslessoutside the contract if the alignment does not match (chapter 38)
integer → pointerimplementation-definedthe provenance is lost (chapter 15)

Table 115.1 — The risk of each conversion

A list of the places quiet conversions happen#

Implicit conversion happens without asking in the following places.

Other conversions often met#