Преобразование между целыми типами

Простые типы / Числа / Integer

long long d1 = LLONG_MAX;
//d1 is 9223372036854775807
//narrowing keeps only the low bits (gcc; the standard leaves it to the
//compiler)
int d2 = (int)d1;
//d2 is -1

long long d3 = 10;
int d4 = (int)d3;
//d4 is 10

//check the range before narrowing
bool fits = d1 >= INT_MIN && d1 <= INT_MAX;
//fits is false

int d5 = INT_MAX;
//d5 is 2147483647
long long d6 = d5;
//d6 is 2147483647, widening is always safe

//signed to unsigned: -1 becomes the largest value
int d7 = -1;
unsigned d8 = (unsigned)d7;
//d8 is 4294967295

printf("d1 is %lld\n", d1);
printf("d2 is %d\n", d2);
printf("d3 is %lld\n", d3);
printf("d4 is %d\n", d4);
printf("fits is %s\n", fits ? "true" : "false");
printf("d5 is %d\n", d5);
printf("d6 is %lld\n", d6);
printf("d7 is %d\n", d7);
printf("d8 is %u\n", d8);