//转换为 int
const char *strNumber = "42";
int number = atoi(strNumber);
//number is 42
//strtol 会显示数字在哪里结束,所以能发现错误的输入
char *end;
long checked = strtol("42abc", &end, 10);
//checked is 42, end is "abc"
//转为 double 和 float
const char *strPi = "3.14";
float pi = strtof(strPi, nullptr);
const char *strExp = "2.71828";
double exp = strtod(strExp, nullptr);
//替换非标准分隔符
char strHalf[] = "0,5";
char *comma = strchr(strHalf, ',');
if (comma != nullptr) {
*comma = '.';
}
double half = strtod(strHalf, nullptr);
printf("number is %d\n", number);
printf("checked is %ld, end is %s\n", checked, end);
printf("pi is %g\n", pi);
printf("exp is %g\n", exp);
printf("half is %g\n", half);