#include #include #include static void run_strtod (const char * input) { double output; char * end; printf ("With strtod, '%s'", input); output = strtod (input, & end); if (end == input) { printf (" is not a valid number.\n"); } else { printf (" becomes %g\n", output); } } static void run_atof (const char * input) { printf ("With atof, '%s' becomes %g.\n", input, atof (input)); } int main () { const char * number = "42.5"; /* Both strtod and atof can cope with whitespace before the number. */ const char * whitespace_number = " 747.149e9"; const char * zero = "0"; const char * not_a_number = "cat"; /* The function atof can convert strings into doubles, but it doesn't distinguish between "zero" and failure. */ run_atof (number); run_atof (whitespace_number); run_atof (zero); run_atof (not_a_number); /* Use a function to run on each of these. */ run_strtod (number); run_strtod (whitespace_number); run_strtod (zero); run_strtod (not_a_number); return 0; }