Converting to a number

Basic types / Strings

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

//strtol shows where the number ends, so bad input is visible
char *end;
long checked = strtol("42abc", &end, 10);
//checked is 42, end is "abc"

//to double and float
const char *strPi = "3.14";
float pi = strtof(strPi, nullptr);

const char *strExp = "2.71828";
double exp = strtod(strExp, nullptr);

//replacement of non-standard delimiter
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);