- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: large integer in C
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-08-2007 08:07 PM
тАО02-08-2007 08:07 PM
large integer in C
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.
- Tags:
- integer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-08-2007 08:24 PM
тАО02-08-2007 08:24 PM
Re: large integer in C
18 446 744 073 709 551 616
or unsigned long if you compile in 64 bits mode.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-08-2007 08:26 PM
тАО02-08-2007 08:26 PM
Re: large integer in C
else be sure to use "%lld"
or unsigned long long X=3563739344804125699LL;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-08-2007 08:38 PM
тАО02-08-2007 08:38 PM
Re: large integer in C
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-08-2007 08:42 PM
тАО02-08-2007 08:42 PM
Re: large integer in C
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".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-09-2007 03:01 AM
тАО02-09-2007 03:01 AM
Re: large integer in C
# 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-09-2007 05:15 AM
тАО02-09-2007 05:15 AM
Re: large integer in C
#include
main(void)
{
unsigned long long big;
big = ~0;
printf("big = %llu\n", big);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-09-2007 06:48 AM
тАО02-09-2007 06:48 AM
Re: large integer in C
Exactly. This is not COBOL. When you do arithmetic, arithmetic conversions are first done, since both are ints, there are no promotions.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-12-2007 12:43 AM
тАО02-12-2007 12:43 AM
Re: large integer in C
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 !*/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-12-2007 01:01 AM
тАО02-12-2007 01:01 AM
Re: large integer in C
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