liveBook · Manning

Integer promotion

As explained in Modern C

Integer promotion is the C conversion of a narrow integer type to int or another integer type before certain operations such as arithmetic.

Consider a char or bool used in an arithmetic expression. Although both are integer types, neither is used directly for arithmetic: each narrow integer type is promoted first, typically to signed int with the same value. section 5.2

The rule has three important consequences:

  • Promotion occurs before arithmetic and comparison. section 5.2
  • The promoted value is usually a signed int, regardless of whether the narrow type was signed. section 5.2
  • The exact widths and ranges of the wider types remain implementation-defined, so platform assumptions require care. section 5.2
Table 5.1. Base types according to the four main type classes. Types with a gray background don’t allow for arithmetic; they are promoted before doing arithmetic. Type char is special since it can be unsigned or signed, depending on the platform. All types in this table are considered to be distinct types, even if they have the same class and precision. (view table figure)

Class

Systematic name

Other name

Rank

Integers Unsigned _Bool bool 0
unsigned char 1
unsigned short 2
unsigned int unsigned 3
unsigned long 4
unsigned long long   5
[Un]signed char 1
  signed char 1
  signed short short 2
Signed signed int signed or int 3
  signed long long 4
  signed long long long long 5
Floating point Real float    
double    
long double    
Complex float _Complex float complex
double _Complex double complex
long double _Complex long double complex

Use the base-type table to distinguish narrow types, such as bool, unsigned char, and short, from unpromoted integer types. section 5.2

How Modern C covers it

How deep it goes
Defines the promotion rule and its typical signed-int result, but defers detailed platform-dependent widths and ranges.
Examples
Uses char and bool as prominent narrow types and classifies base types in a tabular overview.
What it assumes
Assumes familiarity with C base types, signedness, precision, and value ranges.
What you can do after
Evaluate arithmetic and comparison expressions involving narrow integer types with the promotion rule in mind.
How it connects
Connects narrow integer types with arithmetic, comparison, char, bool, signed int, and implementation-defined type ranges.

Also explained in

Compare how 2 books explain integer promotion →

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.