c - Scanf with a specific format -
i allow user put input in specific format.
the format:
a=1,b=-2,c=3
example. spaces allowed inbetween commas , characters.
i'm using: if (scanf("a=%lf,b=%lf,c=%lf",&a,&b,&c) == 1)
reason doesn't work. how can fix it?
you converting 3 numbers, return value should 3 if conversions successful. note %lf
ignores spaces before number. if want ignore spaces around ,
, before =
or a
, add space in format string:
double a, b, c; if (scanf(" =%lf , b =%lf , c =%lf", &a, &b, &c) == 3) { /* conversion successful, 3 numbers parsed */ ... }
note scanf()
not ignore space characters, ignore , whitespace characters, including newlines, tabs, etc.
Comments
Post a Comment