can't get string conversion strtod in c -
could me(sorry english), im trying convert string double when can't here's code(thank you, i'll appreciate much):
#include <stdio.h> #include <string.h> #include <stdlib.h> #define max_long 5 char bx[max_long]; double cali=0; int main() { scanf("%c",bx); cali = strtod(bx,null); printf("%f",cali); return 0; }
when input value greater 10 in output print first number this:
input: 23 output: 2.00000 input: 564 output: 5.00000
the scanf()
specifier using wrong, unless specify number of characters, array not nul
terminated, suggest following
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char bx[6]; /* if want 5 characters, need array of 6 * because string needs `nul' terminator byte */ double cali; char *endptr; if (scanf("%5s", bx) != 1) { fprintf(stderr, "`scanf()' unexpected error.\n"); return -1; } cali = strtod(bx, &endptr); if (*endptr != '\0') { fprintf(stderr, "cannot convert, `%s' `double'\n", bx); return -1; } printf("%f\n", cali); return 0; }
Comments
Post a Comment