It's because the i*2
is integer multiply. Even though you're storing it in a long long
, you're still doing integer math, which causes an overflow.
The following code works, as we promote it up to long long
multiply
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
unsigned int i=4294967294;
unsigned long long j=((unsigned long long)i)*2;
printf("Sizeof i=%d\n", sizeof(i));
printf("Sizeof J=%d\n", sizeof(j));
printf("2xi=%llu\n", j);
return 0;
}
Result:
bash-4.1$ gcc long.c
bash-4.1$ ./a.out
Sizeof i=4
Sizeof J=8
2xi=8589934588