Go Premium for a chance to win a PS4. Enter to Win

x
Solved

# Checking For An Integer Out Of Range

Posted on 1998-09-29
Medium Priority
290 Views
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
Question by:larockd
• 8
• 8
• 2
• +2

LVL 8

Accepted Solution

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

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

LVL 85

Expert Comment

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

LVL 8

Expert Comment

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

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 85

Expert Comment

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

LVL 3

Expert Comment

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

LVL 85

Expert Comment

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

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 85

Expert Comment

ID: 1253117
neither does strlen
0

LVL 3

Expert Comment

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 85

Expert Comment

ID: 1253119
because "the user enters a number into a string"
0

LVL 3

Expert Comment

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

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 85

Expert Comment

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

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

LVL 85

Expert Comment

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

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 85

Expert Comment

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

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

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: Iâ€™ve never triâ€¦
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were smallâ€¦
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
###### Suggested Courses
Course of the Month10 days, 7 hours left to enroll