Solved

Checking For An Integer Out Of Range

Posted on 1998-09-29
20
287 Views
Last Modified: 2010-04-15
I am in a project where the user enters a number into a string.  Before I convert this string to an integer I would like to check to see if it exceeds the defined maximum integer limit set in limits.h.

If you try and convert an integer out of range it will overflow.

I have tried several methods and have no been able to find a successful solution.

Any suggestions
Darrell
0
Comment
Question by:larockd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 8
  • 2
  • +2
20 Comments
 
LVL 8

Accepted Solution

by:
Answers2000 earned 10 total points
ID: 1253108
1. double usually has a wider range than int

2. you want to make sure any comparisons are done using double against double

3. Example

double d = atof( string ) ;
double maxint = MAX_INT ;

if ( d > maxint ) { /* Too large */ }


0
 
LVL 8

Expert Comment

by:jhurst
ID: 1253109
I would suggest even using (float) rather than (double) also the step 3) should use a cast,
0
 
LVL 84

Expert Comment

by:ozo
ID: 1253110
char *string="1e99"
double d = atof( string ) ;
if( d > maxint ){ /* Too large */
  long i = atol(string); /* not too large */
}
/* On The Other Hand: */
char *string="9999999999999999999e-99"
double d = atof( string ) ;
if( d > maxint ){ /* Too large */
}else{
   long i=atol(string); /* also too large */
]

/* why not: */
long i=strtoul(string,0,10);
if( errno ){ perror("\n"); }
 
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:Answers2000
ID: 1253111
jhurst -
1. omitted the cast as we're in C (does no harm though)
2. atof returns double, hence the choice
0
 
LVL 3

Expert Comment

by:arnond
ID: 1253112
after getting the nuber in the string, first check that the length of the string is shorter then the number of digits in MAXINT, then check each digit starting from the highest to the lowest by devidind MAXINT by powers of 10. this way you don't need to use anything else BUT 'int's and your string...
0
 
LVL 84

Expert Comment

by:ozo
ID: 1253113
assuming the string is all digits and the length of MAXINT, you an do a strcmp
0
 
LVL 3

Expert Comment

by:arnond
ID: 1253114
ozo,
why should the string be the same length of MAXINT ?
0
 
LVL 84

Expert Comment

by:ozo
ID: 1253115
Because someone said that after getting the nuber in the string, you first check that the length of the string is shorter then the number of digits in MAXINT...
0
 
LVL 3

Expert Comment

by:arnond
ID: 1253116
ozo, sorry to disapoint you, but strcmp doesn't take 'const int' (such as MAXINT) as a parameter but only strings...
0
 
LVL 84

Expert Comment

by:ozo
ID: 1253117
neither does strlen
0
 
LVL 3

Expert Comment

by:arnond
ID: 1253118
I know, but you don't need either. there are many ways to check the length of an integer. you can devide the integer by 10 time after time and when you get a 0 resault, you know the length (the number of divisions, minus one). so why use any "str..." functions ?
0
 
LVL 84

Expert Comment

by:ozo
ID: 1253119
because "the user enters a number into a string"
0
 
LVL 3

Expert Comment

by:arnond
ID: 1253120
I know, but you don't need either. there are many ways to check the length of an integer. you can devide the integer by 10 time after time and when you get a 0 resault, you know the length (the number of divisions, minus one). so why use any "str..." functions ?
0
 
LVL 3

Expert Comment

by:arnond
ID: 1253121
ozo, please re-read my first entry. the process I suggested will guarantee the relation between the number in the string and MAXINT. after making sure that MAXINT is bigger, larockd can use atoi.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1253122
I never said that the procees you suggested wouldn't guarantee the relation.
I merely mentioned another process.
0
 
LVL 3

Expert Comment

by:arnond
ID: 1253123
but your process doesn't work !
anyway, do you think larockd is still around ?
0
 
LVL 84

Expert Comment

by:ozo
ID: 1253124
I stated certain restrictions necessary for strcmp to work.
Did you wish to add any furthur restrictions?

I don't know whether larockd is still around.

(By the way, I think the name of the maximum value for an object of type int in <limits.h> is INT_MAX)
0
 
LVL 3

Expert Comment

by:arnond
ID: 1253125
the only restrictions i've added is the use of INTs and strings.
you are right, INT_MAX is the correct name.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1253126
I thought the use if int's and strings was implicit in the question.
Why do you say that my process doesn't work?
0
 

Expert Comment

by:JHandby
ID: 1253127
You could use strtol() to convert it to a long. If that fails because the number is out of range, strtol() will return LONG_MAX or LONG_MIN, so you can stop right there. If it succeeds, you just need to check the returned long against INT_MAX if sizeof(int) < sizeof(long int) on your platform.

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question