473,386 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 3682
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Jim Cook | last post by:
We have a macro which takes various index constants as an argument and offsets into an array. The macro can be an Lvalue or Rvalue. The index is not zero based. I would like a compile time error...
26
by: Sterten | last post by:
when I define int R; and then later access it with x=R;C=7; .... but x happens to be <0 or >99 , then the program's behavious becomes unpredictable. Is there a way to prevent this ? Is...
3
by: pocmatos | last post by:
Hi all, I'm doing parsing with flex and bison and I read numbers which are just a sequence of digits + and keep them in an int, however if the number is bigger than int, I should output error....
11
by: Bo Peng | last post by:
Dear C++ experts, I need to store and retrieve a meta information that can be int or double. The program would be significantly simpler if I can handle two types uniformly. Right now, I am using...
8
by: A. Farber | last post by:
Hello, I have this simple program: #include <stdio.h> #include <stdlib.h> int main() { char *args = "2162508224"; printf("args=%s, atoi=%lu, atol=%lu\n",
125
by: jacob navia | last post by:
We hear very often in this discussion group that bounds checking, or safety tests are too expensive to be used in C. Several researchers of UCSD have published an interesting paper about this...
130
by: euler70 | last post by:
char and unsigned char have specific purposes: char is useful for representing characters of the basic execution character set and unsigned char is useful for representing the values of individual...
12
by: aarklon | last post by:
Is y >(8 * (sizeof(int) -1)) portable expression to find The MSB of an unsigned integer y ?? will this work in all the cases, all three of the representations C allows: 1) two's complement...
8
by: perry.yuan | last post by:
Hi Gurus, I am looking for C code for multiplying 32bit by 32bit operands and getting a 64bit product. i.e. U32 m1, m2; U64 p = m1 * m2; Of course I can use
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.