converts a string of digits str into an equivalent integer. - C Function

C examples for Function:Utility Function

Description

converts a string of digits str into an equivalent integer.

#include the file <cype.h>
int atoi(char str[])
{
  int j, p, sign;
  for(j = 0; isspace(str[j]); j++)
    ;                                                 /* null statement */
  sign = (str[j] == '-') ? -1 : 1;
  if(str[j] == '+' || str[j] == '-')
    j++;
  for(p = 0; isdigit(str[j]); j++)
    p = 10 * p + (str[j] - '0');
  return sign * p;
}

Related Tutorials