469,626 Members | 1,045 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,626 developers. It's quick & easy.

checking for > INT_MAX

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?

Nov 14 '05 #1
2 3408
Marlene Stebbins <ma*****@mail.com> writes:
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?


It's hard to tell, since you don't show us how x gets its value. The
checking needs to occur in the code that you didn't post.

If you use something like atoi(argv[i]) to convert a command-line
argument to an int, it won't do any error checking. You can use the
strtol() function, which returns a long int and does error checking if
you give it the right arguments. You'll need to use strtol()'s own
error checking mechanism and check whether the long result fits in an
int. Something like this:

long result = strtol(argv[i], blah, 10);
if (/* strtol indicates an error*/) {
/* error */
}
else if (result > INT_MAX) {
/* overflow */
}
else if (result < INT_MIN) {
/* underflow */
}
else {
/* You have a valid int value */
}

I'm too lazy to look up the details of how to use strtol(); you should
have documentation that will tell you everything you need to know.

Remember that strtol() converts the initial portion of its string
argument, so if you give it "123xyz", it will give you 123. Decide
whether to treat this as an error (strtol() can tell you whether it
converted the entire string or just an initial substring).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #2
Marlene Stebbins wrote:

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?


If you want input from a file stream, see txtio.zip on my site (URL
in sig below) for techniques. If you want numerical input from a
string, see the strtoxx family of routines.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Jim Cook | last post: by
26 posts views Thread by Sterten | last post: by
3 posts views Thread by pocmatos | last post: by
8 posts views Thread by A. Farber | last post: by
125 posts views Thread by jacob navia | last post: by
130 posts views Thread by euler70 | last post: by
12 posts views Thread by aarklon | last post: by
8 posts views Thread by perry.yuan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.