A
arnuld
I know we strtol() converts to long but I want to convert to an int. atoi
() does not detect errors so I don't think its advised to use it. I am
using strtol() anyway but putting check for INT_MAX and INT_MIN values. I
can not check for overflow/underflow because INT_MIN,MAX and LONG_MIN,MAX
have same values on my system. Can someone check of this behaves
correctly ?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
enum { VAL_SUCC = 0,
VAL_ERR = -1
};
int str_to_int(const char* , int* );
int main(void)
{
const char* x = "-2147483648";
int i = 0;
printf(" i = %d, x = %s\n", i, x);
if( 0 > str_to_int(x, &i))
{
printf("IN: %s @%d: ERROR in Conversion :%s\n", __func__, __LINE__,
__FILE__);
}
printf(" i = %d, x = %s\n", i, x);
printf("INT_MAX = %d\n", INT_MAX);
printf("LONG_MAX = %ld\n", LONG_MAX);
printf("LONG_MIN = %ld\n", LONG_MIN);
return 0;
}
int str_to_int(const char* str, int* ip)
{
char *endptr;
long val;
int ret;
errno = 0; /* To distinguish success/failure after call */
val = strtol(str, &endptr, 10);
/* Check for various possible errors */
if ((ERANGE == errno && (LONG_MAX == val || LONG_MIN == val)) ||
((0 != errno) && (0 == val)))
{
perror("strtol");
ret = -1;
}
else if(endptr == str)
{
fprintf(stderr, "No digits were found\n");
ret = -1;
}
else /* If we got here, strtol() successfully parsed a number */
{
printf("strtol() returned %ld\n", val);
if ('\0' != *endptr) /* Not necessarily an error... */
{
printf("Further characters after number: %s\n", endptr);
}
if(INT_MAX < val || INT_MIN > val)
{
printf("IN: Value is greater/lower than what int can handle.
Can not represent it in an integer\n");
ret = -1;
}
else
{
*ip = val;
ret = 0;
}
}
return ret;
}
=========================== OUTPUT ===========================
~/programs/C $ ./a.out
i = 0, x = -2147483648
strtol() returned -2147483648
i = -2147483648, x = -2147483648
INT_MAX = 2147483647
LONG_MAX = 2147483647
LONG_MIN = -2147483648
~/programs/C $
() does not detect errors so I don't think its advised to use it. I am
using strtol() anyway but putting check for INT_MAX and INT_MIN values. I
can not check for overflow/underflow because INT_MIN,MAX and LONG_MIN,MAX
have same values on my system. Can someone check of this behaves
correctly ?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
enum { VAL_SUCC = 0,
VAL_ERR = -1
};
int str_to_int(const char* , int* );
int main(void)
{
const char* x = "-2147483648";
int i = 0;
printf(" i = %d, x = %s\n", i, x);
if( 0 > str_to_int(x, &i))
{
printf("IN: %s @%d: ERROR in Conversion :%s\n", __func__, __LINE__,
__FILE__);
}
printf(" i = %d, x = %s\n", i, x);
printf("INT_MAX = %d\n", INT_MAX);
printf("LONG_MAX = %ld\n", LONG_MAX);
printf("LONG_MIN = %ld\n", LONG_MIN);
return 0;
}
int str_to_int(const char* str, int* ip)
{
char *endptr;
long val;
int ret;
errno = 0; /* To distinguish success/failure after call */
val = strtol(str, &endptr, 10);
/* Check for various possible errors */
if ((ERANGE == errno && (LONG_MAX == val || LONG_MIN == val)) ||
((0 != errno) && (0 == val)))
{
perror("strtol");
ret = -1;
}
else if(endptr == str)
{
fprintf(stderr, "No digits were found\n");
ret = -1;
}
else /* If we got here, strtol() successfully parsed a number */
{
printf("strtol() returned %ld\n", val);
if ('\0' != *endptr) /* Not necessarily an error... */
{
printf("Further characters after number: %s\n", endptr);
}
if(INT_MAX < val || INT_MIN > val)
{
printf("IN: Value is greater/lower than what int can handle.
Can not represent it in an integer\n");
ret = -1;
}
else
{
*ip = val;
ret = 0;
}
}
return ret;
}
=========================== OUTPUT ===========================
~/programs/C $ ./a.out
i = 0, x = -2147483648
strtol() returned -2147483648
i = -2147483648, x = -2147483648
INT_MAX = 2147483647
LONG_MAX = 2147483647
LONG_MIN = -2147483648
~/programs/C $