J
JS
#include <ctype.h>
double atof(char s[]){
double val, power;
int i, sign;
for (i = 0; isspace(s); i++)
;
sign = (s == '-') ? -1 : 1;
if (s == '+' || s == '-')
i++;
for (val = 0.0; isdigit(s); i++)
val = 10.0 * val + (s - '0');
if (s == '.')
i++;
for (power = 1.0; isdigit(s); i++){
val = 10.0 * val + (s - '0');
power *= 10.0;
}
return sign * val / power;
}
The first for loop never terminates and I guess nothing is executed in its
body.
How should (s - '0') be understood? Have never seen a char subtracted
from another char before.
JS
double atof(char s[]){
double val, power;
int i, sign;
for (i = 0; isspace(s); i++)
;
sign = (s == '-') ? -1 : 1;
if (s == '+' || s == '-')
i++;
for (val = 0.0; isdigit(s); i++)
val = 10.0 * val + (s - '0');
if (s == '.')
i++;
for (power = 1.0; isdigit(s); i++){
val = 10.0 * val + (s - '0');
power *= 10.0;
}
return sign * val / power;
}
The first for loop never terminates and I guess nothing is executed in its
body.
How should (s - '0') be understood? Have never seen a char subtracted
from another char before.
JS