Why does this cause an overflow?
It's my understanding that uint64_t defined by C99 (stdint.h) is defined to be 8 bytes (= 64 bits) of length, thus allowing for a maximum value of 2^64 - 1. However, when I try the following code snippet, the uint64_t overflows, even though it's nowhere ne开发者_运维技巧ar 2^64 - 1:
uint64_t Power10(int exponent)
{
int i = 1;
uint64_t ret = 10;
while(i < exponent)
{
ret *= 10;
++i;
}
return ret;
}
Help would be very much appreciated.
You need to print with "%" PRIu64
conversion. Don't forget to add the right include!
#include <inttypes.h>
int main(void) {
printf("Power10(12) is %" PRIu64 "\n", Power10(12));
return 0;
}
精彩评论