liveBook · Manning

Implicit conversion

As explained in Modern C

An implicit conversion is an automatic C conversion that changes a value from one type to another without an explicit cast.

Consider assignments such as `double a = 1;` and `signed int c = 0x80000000;`. C silently converts the right-hand value to the declared type, but the result is safe only when the value fits. A conversion can instead lose information, become implementation-defined, or expose a signedness hazard. section 5.4

double          a = 1;             // Harmless; value fits type
signed short    b = -1;            // Harmless; value fits type
signed int      c = 0x80000000;    // Dangerous; value too big for type
signed int      d = -0x80000000;   // Dangerous; value too big for type
signed int      e = -2147483648;   // Harmless; value fits type
unsigned short  g = 0x80000000;    // Loses information; has value 0


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":6}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":12}],[{\"line\":5,\"ch\":2},{\"line\":5,\"ch\":14}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":10}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":10}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":10}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":10}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":10}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":10}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":10}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":10}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":10}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":14}]]"}
!@%STYLE%@!

Compare harmless, dangerous, and information-losing conversions side by side. section 5.4

Read each conversion from the operand types to the resulting value:

  • Unary `+` and `-` have the type of their promoted argument, so `-1U` is an unsigned value: a large positive integer rather than a negative one. section 5.4
  • Decimal and hexadecimal literals can differ: `-2147483648` can fit a signed `int`, whereas `-0x80000000` effectively remains the unsigned value `2147483648`. section 5.4
  • Mixed signedness changes comparisons: `-1 < 0` is true, but `-1 < 0U` is false because the operands undergo different conversion rules. section 5.4
  • Pointer conversions are narrower: data pointers may convert to or from `void*`, or add a qualifier, but incompatible pointer types remain erroneous or dangerous. section 12.5

Inspecting object bytes

A conversion to `unsigned char*` must be explicit when inspecting an object's representation. Casting the address of an unsigned integer, then indexing through the resulting pointer, exposes its bytes; the observed order depends on the platform. section 12.5

How Modern C covers it

How deep it goes
Explains value fitting, literal typing, signedness, precision loss, implementation-defined results, and pointer restrictions through worked examples.
Examples
Uses typed initializations, hexadecimal literals, mixed-sign comparisons, pointer assignments, and byte inspection of an unsigned integer.
What it assumes
Assumes familiarity with C types, literals, operators, assignments, and pointers while reinforcing promoted types and pointer compatibility.
What you can do after
Evaluate conversion safety by checking value ranges, operand signedness, precision, pointer compatibility, and whether behavior is implementation-defined or undefined.
How it connects
Connects implicit conversion with integer promotions, unary operators, signedness, pointer types, void pointers, qualifiers, and object representation.

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. The section links point to the parts of the book it draws on.