1753361 Members
5148 Online
108792 Solutions
New Discussion юеВ

Re: large integer in C

 
Clutier_1
Occasional Advisor

large integer in C

Hello,
I tri to work with big integers in a C program.
How can i do to manage numbers like 3 563 739 344 804 125 699 ?

I tried to use "unsigned log long" type variables, but it doesn't work correctely ?
(on a RP 5470 server, HPUX 11.O)
Thanks for help.
12 REPLIES 12
Laurent Menase
Honored Contributor

Re: large integer in C

unsigned long long should work up to
18 446 744 073 709 551 616

or unsigned long if you compile in 64 bits mode.
Laurent Menase
Honored Contributor

Re: large integer in C

do you have an example?

else be sure to use "%lld"
or unsigned long long X=3563739344804125699LL;
Georg Tresselt
Honored Contributor

Re: large integer in C

Clutier_1
Occasional Advisor

Re: large integer in C

I tried this :

unsigned long long toto;
...
toto=256*256*256*256; /* this should be 4 294 967 296 */
printf ("%lld",toto);

when i run "cc -o myprog myprog.c", i get an error :

"warning 559: constant expression overflow during evaluation".

Don Morris_1
Honored Contributor

Re: large integer in C

Don't ask me exactly why -- but from personal experience, you have to define at least one of those constants as ULL:

# more toto.c
#include
#include

int
main(int argc, char *argv[])
{
unsigned long long toto;

toto = 256ULL*256*256*256;
printf("%lld\n", toto);

exit(EXIT_SUCCESS);
}

will work, for example. Otherwise, the compiler seems to treat them as ints, so the product stays an int -- then gets promoted via implicit cast to unsigned long long [instead of being implicitly unsigned long long as the product is built].

Dennis may pipe in with exactly what's going on / why this is the way it is / whatnot -- I'm simply reporting what I've observed.
Sandman!
Honored Contributor

Re: large integer in C

Works okay on my HP-UX 11.0 machine:


#include

main(void)
{
unsigned long long big;

big = ~0;
printf("big = %llu\n", big);
}
Dennis Handly
Acclaimed Contributor

Re: large integer in C

>Don: Don't ask me exactly why -- but from personal experience, you have to define at least one of those constants as ULL:

Exactly. This is not COBOL. When you do arithmetic, arithmetic conversions are first done, since both are ints, there are no promotions.
Clutier_1
Occasional Advisor

Re: large integer in C

Thanks to everybody,

The solution is :

- To use the 'ULL' for constants,
- To cast every time u use your unsigned long long variable.

example :
unsigned long long myvar;

....

myvar=4096*4096*4096*4096 /* it will NOT work correctly */

myvar = (unsigned long long ) 4096*4096*4096 /* this will give a perfect result !*/


Laurent Menase
Honored Contributor

Re: large integer in C

In fact when you do your
X=4096*4096*4096*4096;
once again the calculation is made in int since in C the = is evaluated after the right expression.
Which means that we do 4096 int (32bits)*4096 int (32bits) * 4096 int (32bits) * 4096 int (32bits) which made a result of 0 in int (32bit).
Then this value is promoted to a long long.

So you need once again to make
X=4096LL*4096*4096*4096;
^^^^^^
or even X=4096*4096*4096LL*4096;
^^^^^^
( LL and ULL only makes a difference when you make a comparaison ).
It will work better because the calculation is made in int until we * by a LL constant.
because the promotion is made to make that calcul