Syntax
#include <stdlib.h> int atoi(const char *string);Description
atoi converts a character string to an integer value.
The input string is a sequence of characters that can be interpreted as a numerical value of the specified return type. The function stops reading the input string at the first character that it cannot recognize as part of a number; this character can be the null character that ends the string.
atoi does not recognize decimal points nor exponents. The string argument
for this function has the form: ┌──────────────────────────────────────────────────────────────────────────────┐
│ │
│ >>──┬────────────┬──┬───┬──digits──>< │
│ └─whitespace─┘ ├─+─┤ │
│ └─┴─┘ │
│ │
└──────────────────────────────────────────────────────────────────────────────┘
where whitespace consists of the same characters for which the isspace
function is true, such as spaces and tabs. atoi ignores leading white-space
characters. digits is one or more decimal digits.
atoi returns an int value produced by interpreting the input characters as a number. The return value is 0 if the function cannot convert the input to a value of that type. The return value is undefined in the case of an overflow.
This example shows how to convert numbers stored as strings to numerical values using the atoi function.
#include <stdio.h> #include <stdlib.h> int main(void) { int i; char *s; s = " -9885"; i = atoi(s); /* i = -9885 */ printf("atoi( %s ) = %d\n", s, i); return 0; /**************************************************************************** The output should be: atoi( -9885 ) = -9885 ****************************************************************************/ }Related Information