# string to int - easy question lots of points

How in c do I convert a string to an int.
ie '1' too 1.  Thus allowing to use it in a sum.

A simple question but I need an answer fast.

Thanks
###### Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x

Commented:
Error checking is important; atoi() is not good enough unless you are certain that the string contains only digits (and possibly a sign character).  Ditto for strtol().  (If you use strtol(), be sure to have its second argument point to a valid terminating character or '\0'!)  Either of these library functions will return 0 (a VALID VALUE!) if they cannot convert the string to an integer!!!  Beware!

The careful programmer will loop through the string making sure there is nothing except leading whitespace (to be ignored), an optional sign (+ or -), and digits ('0' through '9').  For example:

int  iIndex;
int  iNumber=0;
int  iSign=0;  /* none seen yet */

char cTempChar;
char szMyString[MAX_STRING_LENGTH+1];

.... /* get the string, etc. */

for (iIndex=0;
cTempChar=szMyString[iIndex];
iIndex++)
{ /* loop through whole string */
/* check for digit
| philosophy: most frequent case first
*/
if (isdigit(cTempChar) )
{
/* do the math:
|  since we got a (or another) digit,
|  multiply previous result by 10
|  the character - the character zero
|  == the numeric value
*/
iNumber = (10 * iNumber) +
(cTempChar - '0');
/* ready for next character */
continue;
}

/* check for leading whitespace */
if (isspace(cTempChar) )
{
/* if we have seen any digits, error */
if (iNumber)
{
/* printf error message */
return (ERROR_CODE);
/* or break, etc. */
}
continue;
}

/* check for sign */
if ('+' == cTempChar)
{ /* positive */
if (iSign)
{/* error; we already saw a sign! */
/* printf error message */
return (ERROR_CODE);
/* or break, etc. */
}
iSign = 1;
continue;
}

if ('-' == cTempChar)
{ /* negative */
if (iSign)
{/* error; we already saw a sign! */
/* printf error message */
return (ERROR_CODE); /* or break */
}
iSign = -1;
continue;
} /* end of negative */
} /* end of loop through whole string */

if (iSign)
iNumber *= iSign;

/* use iNumber secure in the knowledge
| that it is truly valid
*/
0

Author Commented:
Edited text of question.
0

Commented:
#include <stdlib.h>
int atoi (const char *str);
0

Commented:
ozo is right, but if you want enhanced error checking, use 'strtol()':

int n;
char* psz = "123";
char* pc;

n = ( int) strtol ( psz, &pc, 10);

if ( *pc)
{
/* error */
}
0

Commented:
> Error checking is important;
Yes, it can be, but we weren't told what kinds of errors c96jg cares about or how they should be handled.
> Ditto for strtol().
And does your code check for any error conditions that jkr's code doesn't?

> /* use iNumber secure in the knowledge
> | that it is truly valid
>  */
Unless there was an overflow...
0

Commented:
try this
sscanf(buffer,"%i",&integervar)
0

Author Commented:
In this case error checking had been done thus I didn't say anything about it.

Thanks all anyway
0