liveBook · Manning

Implicit conversion

As explained in Modern C, Third Edition

An implicit conversion is an automatic C conversion that changes a value from one type to another according to the language's conversion rules.

Implicit conversion automatically changes a value to the type required by an initialization, assignment, function argument, or arithmetic operation. The value may fit harmlessly, lose information, become implementation-defined, or change meaning through signedness. section 5.4 section 18.1.2

A conversion can be harmless—or dangerous

double a = 1; converts exactly to 1.0, while signed short b = -1 preserves its value. In signed int c = 0x8000'0000, the unsigned value is too large for the destination and the result is implementation-defined. In unsigned short g = 0x8000'0000, information is lost and the resulting value is 0. section 5.4

C applies several related conversion mechanisms:

  • Unary + and - use the type of their promoted operand; -1 is signed int, whereas -1U is unsigned int and has a large positive value. section 5.4
  • Integer promotion widens narrow integer types such as bool, char, and short before supported arithmetic or function use. section 18.1.2
  • Default argument conversion changes a float result to double when a variadic function such as printf receives it. section 18.1.2
  • Default arithmetic conversion chooses a common type for binary operators; -1 + 1U converts -1 to unsigned int, producing UINT_MAX before addition. section 18.1.2
Figure 18.1 Upward conversion of some standard arithmetic types. Black arrows conserve values; red arrows (between narrow and wide types) may occur for integer promotion or default argument conversion; blue arrows (from signed to unsigned types) are reduction modulo \(2^{N}\); well-definition of gray arrows (with question marks) depends on the platform; green arrows (from integers to floating types) may lose precision.
figure

Use the conversion diagram to compare value-preserving promotions, modulo reduction from signed to unsigned types, platform-dependent paths, and possible floating-point precision loss. section 18.1.2

How Modern C, Third Edition covers it

How deep it goes
Works through promotions and arithmetic rules, stopping short of a complete conversion table while flagging platform-dependent and precision-losing cases.
Examples
Uses integer and floating initializations, hexadecimal and decimal literals, mixed-sign comparisons, cosf, printf, and complex arithmetic.
What it assumes
Assumes familiarity with C types, literals, initialization, operators, function calls, and signed versus unsigned integers.
What you can do after
Evaluate C expressions and conversions for preserved values, information loss, implementation-defined behavior, signedness hazards, and precision loss.
How it connects
Connects implicit conversion to integer promotion, default argument conversion, default arithmetic conversion, signedness, and floating-point precision.

Also explained in

Compare how 4 books explain implicit conversion →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.