I'm trying to understand the mechanics of the following conversion
#include <stdio.h>
#include <stdint.h>
int main() {
int a = 0xffffffff;
printf("%d", a); // prints -1
return 0;
}
According to integer constant the type of 0xffffffff
is unsigned int
. This can be easily checked by doing printf("%s", 0xffffffff)
;
Now, according to implicit conversion semantics:
"Integer promotion is the implicit conversion of a value of any integer type with rank less or equal to rank of int [...] to the value of type int or unsigned int."
and, as stated below
"the ranks of all signed integer types equal the ranks of the corresponding unsigned integer types"
so the promotion applies, because the rank of unsigned int
is the same as the rank of int
.
That promotion is defined as
"If int can represent the entire range of values of the original type (or the range of values of the original bit field), the value is converted to type int. Otherwise the value is converted to unsigned int"
But, and that is what I don't understand, an int
cannot represent the unsigned int
4,294,967,295, but still it is converted to int
. And that happens without any narrowing warning. Why is it so?