liveBook · Manning

Signedness

As explained in Modern C

Signedness is the property of an integer type that determines whether its representation and value range include negative values.

Signedness determines whether an integer type represents negative values. It also determines the type’s value range and affects how C evaluates expressions that combine integer types. section 5.4 section 5.7.1

The same comparison can change meaning

-1 < 0 // true, same signedness -1U < 0U // false, same signedness -1 < 0U // false, mixed signedness -1L < 0U // depends on platform The mixed cases convert operands before comparison. A negative signed value can therefore become a large unsigned value, while combinations involving different widths can depend on implementation ranges. section 5.4

Keep these distinctions in view:

  • Unsigned integer arithmetic is determined by its precision; with p value bits, its maximum value has the form 2^p − 1. section 5.7.1
  • Signed and unsigned types do not have strictly nested value ranges, so implicit conversion choices can produce surprising results. section 5.4
  • Choose arithmetic types so implicit conversions are harmless, and avoid operations whose operands have different signedness. section 5.4
-1    < 0    // True, harmless, same signedness
-1L   < 0    // True, harmless, same signedness
-1U   < 0U   // False, harmless, same signedness
-1    < 0U   // False, dangerous, mixed signedness
-1U   < 0    // False, dangerous, mixed signedness
-1L   < 0U   // Depends, dangerous, same or mixed signedness
-1LL  < 0UL  // Depends, dangerous, same or mixed signedness

Compare harmless same-signedness expressions with dangerous mixed-signedness cases, including platform-dependent outcomes. section 5.4

How Modern C covers it

How deep it goes
Works through comparisons and ranges, but defers a complete account of signed integer representation.
Examples
Uses comparison expressions, integer declarations, and bounds tables to expose range loss and conversion surprises.
What it assumes
Assumes familiarity with C integer expressions and introduces signed and unsigned ranges through concrete examples.
What you can do after
Diagnose surprising comparisons and select arithmetic types that avoid harmful implicit conversions.
How it connects
Connects signedness with integer types, value ranges, implicit conversions, integer representation, and arithmetic.

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