NoneAtoi () is the C runtime library function for converting the ASCII representation of a number to an integer. This SO tag also applies to atol(), atoll(), and atoq() which perform the same conversion to types "long" and "long long".

Use this tag on for all questions about the use of the atoi() family of functions or where it does not seem to be working correctly.

Closely related are:

  • atof for double
  • strtol for converting to long from text in any base from 2 through 36, with unambiguous error checking. Or choose a base automatically depending on how the number is written. strtoul() converts to an unsigned long.
  • strtod for double
  • scanf for converting one or more values at a time directed by a format specification

SYNPOSIS

#include <stdlib.h>

       int atoi (const char *nptr);
      long atol (const char *nptr);
long long atoll (const char *nptr);
 long long atoq (const char *nptr);

BSD-based libraries may declare the last as

    quad_t atoq (const char *nptr);

These functions all return zero if there is any problem during the conversion. Since converting a zero returns the value of zero, there is no easy way to distinguish an error from a correct conversion of zero. Use the scanf() or strtol() functions if such a check is needed.

Leading whitespace is skipped. An optional leading sign (+ or -) is accepted. The scan stops at the first non integer digit.