• C

# Checking For An Integer Out Of Range

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
###### Who is Participating?

Commented:
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

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

Commented:
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

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

Commented:
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

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

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

Commented:
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

Commented:
ozo, sorry to disapoint you, but strcmp doesn't take 'const int' (such as MAXINT) as a parameter but only strings...
0

Commented:
neither does strlen
0

Commented:
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

Commented:
because "the user enters a number into a string"
0

Commented:
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

Commented:
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

Commented:
I never said that the procees you suggested wouldn't guarantee the relation.
I merely mentioned another process.
0

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

Commented:
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

Commented:
the only restrictions i've added is the use of INTs and strings.
you are right, INT_MAX is the correct name.
0

Commented:
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

Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.