473,729 Members | 2,340 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3711
Marlene Stebbins <ma*****@mail.c om> 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_Keit h) 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********@yah oo.com) (cb********@wor ldnet.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
4963
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 displayed if the index is out of bounds. Is there a way to do this well? I read through the FAQ that I could find mentioned, and did not see this sort of question in the preprocessor section anywhere. What I came up with is below. It does in...
26
1846
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 there a program or debugger or compiler which
3
9352
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. What's the best way to check this? In the flex side I have: {numeral} { yylval.num = strtoul(yytext, NULL, 0); return NUMERAL; }
11
5672
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 a single interface: void setInfo(double); // store info double info(); // retrieve info and use
8
4724
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
6578
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 problem. http://www.jilp.org/vol9/v9paper10.pdf Specifically, they measured the overhead of a bounds
130
6773
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 bytes. The remainder of the standard integer types are general purpose. Their only requirement is to satisfy a minimum range of values, and also int "has the natural size suggested by the architecture of the execution environment". What are the...
12
3641
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 2) ones'complement 3) signed magnitude.
8
5861
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
8761
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9426
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9142
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8148
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2163
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.