Name
atoi
Synopsis
Converts a string to an integer
#include <stdlib.h> intatoi
( const char *s
); longatol
( const char *s
); long longatoll
( 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
Get C in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.