Previous Page
Next Page

atoi

Converts a string to an integer

#include <stdlib.h>
int atoi ( const char *s  );
long atol ( const char *s  );
long long atoll ( const char *s  );         (C99)

The atoi( ) function converts a string of characters representing a numeral into a number of int. Similarly, atol( ) returns a long integer, and in C99, the atoll( ) function converts a string into an integer of type long long.

The conversion ignores any leading whitespace characters (spaces, tabs, newlines). A leading plus sign is permissible; a minus sign makes the return value negative. Any character that cannot be interpreted as part of an integer, such as a decimal point or exponent sign, has the effect of terminating the numeral input, so that atoi( ) converts only the partial string to the left of that character. If under these conditions the string still does not appear to represent a numeral, then atoi( ) returns 0.

Example

char *s = " -135792468.00 Balance on Dec. 31";
printf("\"%s\" becomes %ld\n", s, atol(s));

These statements produce the output:

" -135792468.00 Balance on Dec. 31" becomes -135792468

See Also

strtol( ) and strtoll( ); atof( ) and strtod( )


Previous Page
Next Page