11

I am not sure if promotion just means converting a data type to a larger data type (for example short to int).

Or does promotion means converting a data type to another "compatible" data type, for example converting a short to an int, which will keep the same bit pattern (the extra space will be filled with zeros). And is conversion means converting something like an int to a float, which will create a completely different bit pattern?

3 Answers3

13

There are two things that are called promotions: integral promotions and floating point promotions. Integral promotion refers to integral types (including bitfields and enums) being converted to "larger" integral types and floating point promotion is specifically just float to double.

Both types of promotions are subsets of a wider range of conversions.

  • char -> int: integral promotion
  • float -> double: floating point promotion
  • int -> char: [narrowing] conversion (not a promotion)
  • int -> float: conversion
  • const char* -> std::string: conversion
  • Foo -> Bar: possibly undefined conversion?
  • etc.
Barry
  • 286,269
  • 29
  • 621
  • 977
  • So converting from an integral type to a floating point type is not called promotion (even if the floating point type can represent all of the values of the integral type)? –  Jan 28 '15 at 05:03
  • @rony_t It is not. That would just be a conversion. – Barry Jan 28 '15 at 05:12
  • So conversion means converting between different types and promotion means widening a data type? – Abdel Aleem Apr 21 '22 at 08:58
12

A promotion is a specific kind of conversion for built-in types that is guaranteed not to change the value.

The type you are promoting to must be able to accurately represent any possible value of the type you are promoting from.

Here is a list of the applicable conversions.

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
0

Promotion

char or short values (signed or unsigned) are promoted to int (or unsigned) before anything else happens this is done because int is assumed to be the most efficient integral datatype, and it is guaranteed that no information will be lost by going from a smaller datatype to a larger one

Conversion

after integral promotion, the arguments to an operator are checked if both are the same datatype, evaluation proceeds if the arguments are of different datatypes, conversion will occur

Casts

the type of an expression can be forced using casts. a cast is simply any valid datatype enclosed in parentheses and placed next to a constant, variable or expression

Please Refer to this : website