I am trying to read in a double
value continuously from the user using scanf
.
Code:
printf("Enter A value: \n");
double input;
int result = scanf("%f", &input);
printf("INPUT: %f\n", input);
The output is
INPUT: 0.000
I am trying to read in a double
value continuously from the user using scanf
.
Code:
printf("Enter A value: \n");
double input;
int result = scanf("%f", &input);
printf("INPUT: %f\n", input);
The output is
INPUT: 0.000
You lied to the compiler: when scanning, %f
says you supply a pointer to float
. But you provided a pointer to double
.
To fix, either use %lf
or declare input
as float
.
Note that there is an asymmetry with printf
formats, which uses %f
for both float
and double
arguments. This works because printf
arguments are promoted to double
(and are not pointers).
I am trying to read in a double value continuously from the user using scanf.
To do so you need a loop, like the following:
while(scanf("%lf", &input) == 1) {
//code goes here...
printf("INPUT: %lf\n", input);
//code goes here...
}
Note that, as the primitive type of input
is double
, you need to use %lf
instead of %f
(%f
used for float
).