liveBook · Manning

Integer promotion

As explained in Modern C, Third Edition

Integer promotion is the implicit conversion of a narrow integer type to an integer type before certain C expressions are evaluated.

Integer promotion implicitly replaces a narrow integer with a wider integer before arithmetic is evaluated. In ordinary C implementations, char, _Bool, and short values become signed int values of the same value, regardless of their original signedness. section 5.2

Promotion inside a small expression

#include <stdbool.h>\n#include <stdio.h>\n\nint main(void) {\n char c = 3;\n _Bool ready = true;\n short count = 4;\n\n printf("%d %d %d\\n", c + 1, ready + 1, count + 1);\n}\n\nBefore each addition, the narrow operand is promoted, so these expressions are evaluated using signed int values. The resulting arithmetic values can then be passed to printf. section 5.2 section 18.1.2

Keep these promotion rules in view:

  • Arithmetic does not operate directly on narrow integer types; promotion occurs first. section 5.2
  • Promotion belongs to a larger family of conversions that also determines common types for binary arithmetic. section 18.1.2
  • Do not confuse promotion with every later conversion: -1 + 1U promotes the signed operand first, then converts it to unsigned before addition. section 18.1.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 behave like 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.
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
ClassSystematic nameOther nameRank
IntegersUnsignedbool_Bool0
unsigned char1
unsigned short2
unsigned intunsigned3
unsigned long4
unsigned long long5
[Un]signedchar1
Signedsigned char1
signed shortshort2
signed intsigned or int3
signed longlong4
signed long longlong long5
Floating pointRealfloat
double
long double
Complexfloat _Complexfloat complex
double _Complexdouble complex
long double _Complexlong double complex

Use the base-type table to distinguish narrow types that require promotion from wider types used directly in arithmetic, while noting char's platform-dependent signedness. section 5.2

How Modern C, Third Edition covers it

How deep it goes
Defines the initial promotion step and connects it to common arithmetic conversions, but does not enumerate every conversion rule.
Examples
Uses char, _Bool, and short arithmetic, plus -1 + 1U and cosf(1) passed to printf.
What you can do after
Evaluate arithmetic expressions by identifying narrow operands and the subsequent signed, unsigned, or floating conversion steps.
How it connects
Connects integer promotion with narrow integer types, default argument conversion, default arithmetic conversion, signed integers, and unsigned integers.

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