M
Marlene Stebbins
I am entering numbers into my program from the command line. I want to
check whether they are > INT_MAX. Sounds simple, but I've discovered that
if(x <= INT_MAX)
{
/* use x in some calculation */
}
else
{
/* exit with error message */
}
doesn't work! And, after thinking about it a little bit, it makes sense
that it doesn't work because if x is bigger than INT_MAX (whatever that
might be on a particular implementation) it just gets clobbered into
something that is garbage before it ever gets to my test. Is there a way
to use INT_MAX to check for overflow? How would you best deal with this
problem generally?
check whether they are > INT_MAX. Sounds simple, but I've discovered that
if(x <= INT_MAX)
{
/* use x in some calculation */
}
else
{
/* exit with error message */
}
doesn't work! And, after thinking about it a little bit, it makes sense
that it doesn't work because if x is bigger than INT_MAX (whatever that
might be on a particular implementation) it just gets clobbered into
something that is garbage before it ever gets to my test. Is there a way
to use INT_MAX to check for overflow? How would you best deal with this
problem generally?