liveBook · Manning

Signedness

As explained in Modern C, Third Edition

Signedness is the property of an integer type that determines whether its values include negative numbers or are restricted to nonnegative numbers.

Signedness determines whether an integer type can represent negative values. In C, mixing signed and unsigned operands triggers implicit conversions, and the result can differ from what the source code appears to say. A negative signed value may become a large unsigned value, changing comparisons and arithmetic. section 5.4

A comparison that changes meaning

The comparison -1 < 0 is true, while -1 < 0U is false: the signed -1 is converted to unsigned int before comparison. Comparisons involving wider signed and unsigned types can also depend on platform widths. TAKEAWAY: avoid operations with operands of different signedness, and choose arithmetic types that avoid problematic implicit conversions. section 5.4

double         a =  1;             // Harmless; value fits type
signed short   b = -1;             // Harmless; value fits type
signed int     c =  0x8000'0000;   // Dangerous; value too big for type
signed int     d = -0x8000'0000;   // Dangerous; value too big for type
signed int     e = -2'147'483'648; // Harmless; value fits type
unsigned short g =  0x8000'0000;   // Loses information; has value 0

Use the assignments to compare values that fit their destination types with values that overflow or lose information, including signed and unsigned integer cases. section 5.4

How Modern C, Third Edition covers it

How deep it goes
Explains mixed-sign comparisons through converted values and platform widths, while stopping short of a complete conversion taxonomy.
Examples
Uses signed and unsigned comparisons plus assignments whose values fit, overflow, or lose information.
What it assumes
Assumes familiarity with C integer expressions and comparisons while defining signed and unsigned behavior through examples.
What you can do after
Diagnose/debug mixed-signedness comparisons and select arithmetic types that reduce implicit-conversion surprises.
How it connects
Connects signedness with implicit conversions, integer types, comparisons, arithmetic types, and portability.

Also explained in

Compare how 2 books explain signedness →

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.