The promotions are a subset of conversions performed implicitly in certain circumstances, even if the target type is not given. Here are two of them:
- Many built-in arithmetic operators "promote" their arguments to
int
if they are of lesser rank. The following program prints 256, because bothchar
s are promoted toint
before they are added, and the resulting expression has typeint
; by contrast, the secondprintf
prints 0, because++
does not promote its argument so that the 8 bit value overflows (which is defined for unsigned types).
#include <cstdio>int main(){ unsigned char c1 = 255, c2 = 1; std::printf("%d\n", c1 + c2); std::printf("%d\n", ++c1);}
- Talking about
printf
: Integral arguments of lesser rank thanint
which do not have a corresponding parameter in the function declaration (like the second arguments in theprintf
calls above) are promoted toint
. It is impossible to pass genuinechar
s toprintf
. (Note that in the secondprintf
above, the expression++c1
has typeunsigned char
, with the value 0, which is promoted toint
when the argument is passed, but after the evaluation of the expression.)
Other widening conversions which are not promotions, like in unsigned short c = 'a';
, are performed because the target of an assignment, the type of a formal parameter or an explicit conversion demand it.