开发者

Problem with big numbers in C

开发者 https://www.devze.com 2023-03-10 19:05 出处:网络
why should a code like this should provide a so high result when I give it the number 4293974227 (or higher)

why should a code like this should provide a so high result when I give it the number 4293974227 (or higher)

int main (int argc, char *argv[])
{


unsigned long long int i;

unsigned long long int z = atoi(argv[1]);

unsigned long long int tmp1 = z;


unsigned long long int *numbers = malloc (sizeof (unsigned long long int) * 1000);

for (i=0; tmp1<=tmp1+1000; i++, tmp1++) {

    numbers[i] = tmp1;
    printf("\n%llu - %llu", numbers[i], tmp1);
}

}

Result shou开发者_运维问答ld start with the provided number but starts like this:

18446744073708558547 - 18446744073708558547
18446744073708558548 - 18446744073708558548
18446744073708558549 - 18446744073708558549
18446744073708558550 - 18446744073708558550
18446744073708558551 - 18446744073708558551

ecc...

What's this crap??

Thanks!


atoi() returns int. If you need larger numbers, try strtol(), strtoll(), or their relatives.


atoi() returns (int), and can't deal with (long long). Try atoll(), or failing that atol() (the former is preferred).


You are printing signed integers as unsigned.

0

精彩评论

暂无评论...
验证码 换一张
取 消