Reading in double values with scanf in c
Solution 1:
Use the %lf
format specifier to read a double:
double a;
scanf("%lf",&a);
Wikipedia has a decent reference for available format specifiers.
You'll need to use the %lf
format specifier to print out the results as well:
printf("%lf %lf",a,b);
Solution 2:
As far as i know %d
means decadic which is number without decimal point. if you want to load double value, use %lf
conversion (long float). for printf your values are wrong for same reason, %d
is used only for integer (and possibly chars if you know what you are doing) numbers.
Example:
double a,b;
printf("--------\n"); //seperate lines
scanf("%lf",&a);
printf("--------\n");
scanf("%lf",&b);
printf("%lf %lf",a,b);
Solution 3:
You are using wrong formatting sequence for double
, you should use %lf
instead of %ld
:
double a;
scanf("%lf",&a);