"C Library - a string into integer function atoi"

1.atoi function prototype

int atoi(const char *nptr);

  atoi (represented ascii to integer) is a function of the number string into plastic. int atoi (const char * nptr) function string nptr scan parameters, skips preceding whitespace (e.g., space, tab indentation) and the like. If nptr nptr or can not be converted int an empty string, then 0 is returned.

  Specifically, this function requires that the string is converted to a decimal number appreciated. Atoi string corresponding to the digital input of limitation in the size (the size of the int type), if it is too large may be given -1.

  Note: When numeric characters and other characters mixed, it is necessary to separate the two.

2. Examples

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
    int n;
    char *str = "12345.67";
    n = atoi(str);
    printf("n=%d\n",n);
    return 0;
}

  Output: n = 12345

  Description When it is detected when the character is not a number, return directly.

 

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
    int n;
    char *str = "pp12345.67";
    n = atoi(str);
    printf("n=%d\n",n);
    return 0;
}

  Output: n = 0 

 

Guess you like

Origin www.cnblogs.com/zhuangquan/p/12572730.html