Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

atoi - Convert a string into an integer (int)

&pagelevel(4)&pagelevel

Definition

#include <stdlib.h>

int atoi(const char *s);

atoi converts a string to which s points into an integer. The string to be converted may be formatted as follows:

[{ tab | 'BLANK' }...] [+|-] digit ...

All control characters for white space are legal for tab (see definitiona at isspace).

The atoi(str) function differs from strtol(str,(char**)NULL) only in error handling. 

Return val.

Integer value of type int



for strings formatted as described above and representing a numeric value that lies in the permissible range of integers.


0

for strings which do not correspond to the syntax described above.

 

INT_MAX or INT_MIN

 


in the case of an overflow, depending on the sign.

Notes

atoi() is completely contained in strtol(). However, the function continues to be offered because it is used in many existing applications.
atoi also recognizes strings that begin with digits but then end with any character.
atoi cuts off the numeric part, converts it according to the above description, and ignores the rest.


Example

The following program converts a string passed in the call (Enter Options) into the corresponding floating-point number.

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
               /* Numbers are passed as strings!! A conversion is */ 
               /* required if the numeric value is needed */
{
  printf("floating : %f\n", atof(argv[1]));
  return 0;
}

See also

atoi, atol, strtod, strtol, strtoul