Implicit decimal points in floating-point reads

Brian Fennell fenn at wpi.WPI.EDU
Wed May 22 06:00:03 AEST 1991


In article <5366 at dftsrv.gsfc.nasa.gov> brotzman at nssdca.gsfc.nasa.gov writes:
>   The problem I am encountering is this:  FORTRAN allows input strings
>representing floating point values to have implicit decimal places, i.e.
>the string "26208" read with a format of F5.3 results in a value of 26.208.
>As far as I can tell there is no equivalent functionality in C, I have tried
>using scanf with an input format of "%5.3f", but the result is garbage (see
>below).

I think the answer is "you can't get there from here."

>Please tell me that I'm being a bonehead and missing something obvious.  :-)

no bonehead about it, but since this is something you need a fix for,
not a how-do-I-write-like-a-real-C-Guru-would, try this...

>---------------------------< TEST.C >---------------------------------
>#include <stdio.h>
>
>main()
>{  char a1[10] = "26208";
>   float x = 0.0;
>   int ifld = 0;
>   ifld = sscanf(a1, "%5f", &x); 

    x /= 1000.;	/* simple, to the point, no fancy fix routines needed */

>   printf("%s %f\n%s %i", "input string converted to: ", x,
>            "number of parameters converted: ", ifld);
>}

Brian Fennell == fenn at wpi.wpi.edu



More information about the Comp.lang.c mailing list