scanf - Sscanf and detect input conversion errors [C] -


i want convert program argument double. simple: want pass program double value argument. make right wanted detect conversion errors. found out atof isnt choice, switched sscanf. heres code:

#include <stdio.h> #include <stdlib.h>  int main(int argc, char **argv) {     argc --;     argv++;      double s;     int handle =-1;      handle = sscanf(*argv, "%lf", &s);             if(handle == -1)             {                 fprintf(stderr, "invalid input double!\n");              }else{ printf("%f\n", s);}      return 0; } 

but on wrong input, ie.: ./code blabla prints out 0.0000000000 whats wrong?

use strtod instead of scanf family. returns 0.0 if no number parsed (unfortunately when number 0.0). can use endptr argument check function stopped parsing number, can used check if string contains valid number. sets errno when value can't represented.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -