[After accept correction per @Lundin] comment
On your machine, 0x12345678
is narrower than unsigned long long
- certainly a signed long
or maybe int
.
A signed long * signed long
is still an signed long
and can suffer from signed integer overflow, which is UB. The range of your signed long
is less than the mathematical product of 0x12345678 * 0x12345678
. By using ULL
suffix, the math is done at least with unsigned long long
math. @BLUEPIXY
printf("%llu\n", 0x12345678ULL * 0x12345678);
// or if the constant can not be changed
printf("%llu\n", 1ULL * SOME_BIG_CONSTANT * SOME_BIG_CONSTANT);
Pedantic note: When printing integer types that might wider than int/unsigned
, insure the final computed result matches the specifier. Consider that SOME_BIG_CONSTANT might be wider than unsigned long long
. Or leave the cast off, and cope with the potential compiler warning.
printf("%llu\n", (unsigned long long) (1ULL * SOME_BIG_CONSTANT * SOME_BIG_CONSTANT));
See also Why write 1,000,000,000 as 1000*1000*1000 in C?
and There are reasons not to use 1000 * 1000 * 1000