C literals and overflow

Viewed 42

On my machine, where an int is 32 bits, the following code :

int64_t m = (int64_t) 1 << 60;
int64_t n = (int64_t) 2048 * 2048 * 2048;

gives the mathematically expected results of 2^60 and 2^33 even though the standard seems to say that 1 and 2048 should be treaded as "int".

Should I consider that result as luck, or does the C99 standard guarantees that I don't get overflow?

1 Answers

Casting has precedence on many other operations, you can see the order here http://users.eecs.northwestern.edu/~wkliao/op-prec.htm

So you are actually working with int64_t all the time (as the name says, it's a signed integer with 64 bits) and that's why you don't overflow.

Related