从字符串转换

基本类型 / 数字 / 整型

const char *strNumber = "42";
int number = atoi(strNumber);
//number is 42

//strtol also tells where the number ended
const char *strMixed = "42abc";
char *rest;
long parsed = strtol(strMixed, &rest, 10);
//parsed is 42, rest is abc

//atoi cannot report an error, it simply returns 0
int bad = atoi("abc");
//bad is 0

printf("number is %d\n", number);
printf("parsed is %ld\n", parsed);
printf("rest is %s\n", rest);
printf("bad is %d\n", bad);