liveBook · Manning

Integer

As explained in Modern C, Third Edition

An integer type is a C type whose values represent whole numbers, with either signed or unsigned representation.

A value does not determine its type

The expressions `-1` and `-1U` begin with different integer literals. The decimal literal `1` has a signed type; the suffix `U` forces `1U` to have an unsigned type. The minus sign is an operator applied to the literal, not part of it. Consequently, `-1U` is not a signed negative value. section 5.3

An integer type represents whole numbers. C divides integer types into signed and unsigned classes. Each type has a precision that limits its range, but the C standard constrains rather than fixes the precision of the base types. Signed and unsigned types generally come in corresponding pairs by integer rank; `char` and `bool` are exceptions. section 5.2 section 5.7.5

Rules to keep in mind:

  • Before arithmetic, narrow integer types are promoted. On realistic platforms, this produces a `signed int` with the same value, even when the narrow type is unsigned. section 5.2
  • A decimal integer literal takes the first signed type that fits. A binary, octal or hexadecimal literal may instead take an unsigned type. Use decimal notation for negative values. section 5.3
  • A positive signed value has the same representation as its corresponding unsigned value. C23 requires two’s-complement sign representation for signed integers. section 5.7.5

When spelling changes the type

Suppose `signed int` has a maximum of 32,767. The decimal literal `32768` does not fit, so it has type `signed long`; `-32768` is the negation of that long value. Under the same assumption, `0x8000` can have an unsigned type, making `-0x8000` unsigned too. Never infer a negative value from a hexadecimal bit pattern alone. section 5.3

Table 5.3 Examples for literals and their types, under the supposition that signed and unsigned have the commonly used representation with 32 bits
Literal 𝑥 Value Type Value of -𝑥
2'147'483'647 +2,147,483,647 signed -2,147,483,647
2'147'483'648 +2,147,483,648 signed long -2,147,483,648
4'294'967'295 +4,294,967,295 signed long -4,294,967,295
0x7FFF'FFFF +2,147,483,647 signed -2,147,483,647
0x8000'0000 +2,147,483,648 unsigned +2,147,483,648
0xFFFF'FFFF +4,294,967,295 unsigned +1
1 +1 signed -1
1U +1 unsigned +4,294,967,295

Compare literal values, inferred types and the results of negation under the table’s stated 32-bit assumptions. section 5.3

Integer types also serve specific roles: `size_t` represents sizes and cardinalities, while `ptrdiff_t` represents size differences. In structures, integer bit-fields can specify member widths; choosing a width still requires checking the values that member must hold. section 5.2 section 6.3.4

How Modern C, Third Edition covers it

How deep it goes
Explains type classes, promotions, literal typing and signed representation; treats integer bit-fields as a further storage application.
Examples
Contrasts decimal and hexadecimal literal types, tabulates negation results, and uses time components to illustrate bit-field widths.
What it assumes
Builds on C values and arithmetic expressions while defining integer classes, promotions and literal typing.
What you can do after
Evaluate/understand how C chooses integer literal types and when signedness or width changes the meaning of a value.
How it connects
Connects integers to integer promotions, integer literals, two’s complement, semantic arithmetic types and structure bit-fields.

Also explained in

Compare how 15 books explain integer →

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.