Mail Archives: djgpp/1993/01/08/17:57:44
|<><><><><> Original message from ecm9093 AT pcmail DOT aston DOT ac DOT uk <><><><><>
|
|> (In cases like this, it is hard to tell what he meant...)
|>
|> >Richard Shields <src4src!mcdhup!rutgers!sequent.com!richards> writes:
|>
|> >>
|> >> 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);
|
|In the reference manual, amount should be &amount, i.e. the address
|of the variable.
|
|> >> printf("amount = %10.2f\n", amount);
|> >>
|> >>
|> >> When I run the program and enter 4.25 the printf statement prints 0.00.
|> >>
|>
|> >You told scanf to read in a "float", not a "double". Need to say "%lf".
|>
|> Maybe he meant to declare amount as float.
|>
|> Also, the program would probably work if he put a star after the comma
|> in the printf() call. (Kids, don't try this at home...)
|>
|
|I hope this helps
Probably not. So many wrong answers to such a basic question. The scanf
call fails because it expects a pointer to a float not the value of a
double.
ANSI Conformant code follows.
float f;
double d;
long double ld;
scanf("%f", &f);
scanf("%lf", &d);
scanf("%Lf", &ld);
printf("f = %10.2f\n", f);
printf("d = %10.2lf\n", d);
printf("ld = %10.2Lf\n", ld);
Keep in mind though that DJGPP doesn't support the %Lf format in
scanf and that doubles and long doubles are the same size in DJGPP.
TJ Merritt
tjm AT netcom DOT com
- Raw text -