For functions like printf
that use variadic arguments, any integral types smaller than an int
(char
and short
) are implicitly promoted to int
. The same is true with floating-point numbers, float
is promoted to double
.
Hence, your char
is being sign-extended to an int
with value -1
, and since you are printing it as unsigned, in 2's complement you get UINT_MAX
.
Edit: as chux notes below, if your char
defaulted to unsigned (this depends on your compiler/platform), the answer would be 255
instead. When promotion occurs, the value will be zero-extended instead of sign-extended.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…