Joseph
Joseph

Reputation: 401

Problem with big numbers in C

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 should 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!

Upvotes: 0

Views: 1533

Answers (3)

Carl Norum
Carl Norum

Reputation: 225202

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

Upvotes: 6

Philip Sheard
Philip Sheard

Reputation: 5825

You are printing signed integers as unsigned.

Upvotes: 0

geekosaur
geekosaur

Reputation: 61457

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

Upvotes: 3

Related Questions