Mail Archives: djgpp/1993/01/07/17:04:01
> Has anyone had any trouble using scanf to read in a floating point value?
>
> I tried this in my code:
>
> double amount;
>
> scanf("%f", amount);
> printf("amount = %10.2f\n", amount);
>
>
> When I run the program and enter 4.25 the printf statement prints 0.00.
>
> I have successfully used scanf to read in a string but anything else seems
> to fail.
>
> -Rich
Unlike printf(), scanf() needs to be passed a pointer to the variable
you are assigning. You were able to read a string because a string is
a pointer to char. Change your program to say
scanf("%f", &amount);
and all should work fine.
--
Eric Backus
ericb AT lsid DOT hp DOT com
(206) 335-2495
- Raw text -