I have been trying to figure out how to deal with any situation when the result is bigger than what a integer declared can hold. For example:

/*
 * Old_guy.c
 *
 */

 #include <stdio.h>
 
int main()
{
     unsigned long int  hartbits;
     
 /* years * days * hours * minutes * seconds */    
     hartbits = 90 * 365 * 24 * 60 * 60;  /* approx. bits a 90 years old hart has done */
     
     printf("%d", hartbits); /* integer overflow */
     
     getchar();
     return(0);
}

In this case a double makes not difference neither. How does programmers if they need to compute something like 1*2*3*4*5*6...*45?
No knowing this, is bugging me.

Recommended Answers

All 4 Replies

When you know that you need values bigger than any type can hold, you use something like GMP to handle arbitrary precision.

Member Avatar for iamthwee

If you want to test that you might need ultimate zip to unzip it.

I have been trying to figure out how to deal with any situation when the result is bigger than what a integer declared can hold.

I believe your actual question was addressed with GMP.

But with the code you posted, a good compiler or linter ought to tell you something like this (since it is a compile-time constant):

../main.c:13: warning: integer overflow in expression

For run-time checking of standard types, you can check for overflow something like this:
Checking for Integer Overflow

/FWIW

Thank you guys to all of you. I got to read the documentation about GMP so I can understand better.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.