www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1993/01/08/17:57:44

To: J DOT P DOT Fletcher AT aston DOT ac DOT uk
Cc: djgpp AT sun DOT soe DOT clarkson DOT edu
Subject: Re: scanf woes
Organization: Code Generation Technology, San Francisco, CA
Date: Fri, 08 Jan 93 14:52:40 -0800
From: "Thomas J. Merritt" <tjm AT netcom DOT com>

|<><><><><> 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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019