jsmng@csune.cs.uh.edu (09/15/89)
I used the following program trying to read in 5 numbers interactively.
As a test, I typed in 1.0 1.0 1.0 1.0 1.0 as input but the output is
about 0.47 (approximately I forget the exact value but it is far from 1).
When I changed the type of vec[] to be float. Everything is fine.
Does anyone have any experience with this? According to K&R, %f for
double should be allowed in scanf. We are running SunOS 4.0.1 and
I think our C compiler follows the K&R standards (as opposed to
ANSI).
#include <stdio.h>
#define V 5
main()
{
double vec[V];
int i;
for (i=0;i<V;i++)
scanf("%f ",&vec[i]);
for (i=0;i<V;i++)
printf("%f ",vec[i]);
}
gwyn@smoke.BRL.MIL (Doug Gwyn) (09/15/89)
In article <12988@uhnix1.uh.edu> jsmng@csune.cs.uh.edu () writes: >According to K&R, %f for double should be allowed in scanf. Nope. %f is used for float, not double, in scanf format specs. Use %lf for double.
henry@utzoo.uucp (Henry Spencer) (09/16/89)
In article <12988@uhnix1.uh.edu> jsmng@csune.cs.uh.edu () writes: >... According to K&R, %f for double should be allowed in scanf... Please read K&R more carefully. In scanf, the specification for double is %lf. In *printf*, %f works for either float or double, but in *scanf*, you have to make the distinction. -- V7 /bin/mail source: 554 lines.| Henry Spencer at U of Toronto Zoology 1989 X.400 specs: 2200+ pages. | uunet!attcan!utzoo!henry henry@zoo.toronto.edu
cpcahil@virtech.UUCP (Conor P. Cahill) (09/16/89)
In article <12988@uhnix1.uh.edu>, jsmng@csune.cs.uh.edu writes: > Does anyone have any experience with this? According to K&R, %f for > double should be allowed in scanf. This is not true. %f is allowed for doubles in printf since float parameters are automatically converted to doubles, but for scanf() you must use %lf for doubles. See K&R1 pg 149, K&R2 pg 158, or RTFM scanf(2) man page. -- +-----------------------------------------------------------------------+ | Conor P. Cahill uunet!virtech!cpcahil 703-430-9247 ! | Virtual Technologies Inc., P. O. Box 876, Sterling, VA 22170 | +-----------------------------------------------------------------------+
ok@cs.mu.oz.au (Richard O'Keefe) (09/16/89)
In article <12988@uhnix1.uh.edu>, jsmng@csune.cs.uh.edu writes: >[having trouble with scanf("%f", &double_var)] > Does anyone have any experience with this? According to K&R, %f for > double should be allowed in scanf. We are running SunOS 4.0.1 and > I think our C compiler follows the K&R standards (as opposed to ANSI). K&R 1st edition section 7.4 page 149: f a floating-point number is expected; the corresponding argument should be a pointer to a FLOAT. ... the conversion characters e or f may be preceded by l to indicate that a pointer to DOUBLE rather than a pointer to FLOAT is in the argument list. Ever since K&R1 it has been the case that %e and %f in scanf() required pointer-to-float in the argument list, while if the argument list had pointer-to-double the format had to be %le or %lf. X3J11 didn't change that. [I cite the Old Testament here because the original poster appealed to it.]
MARWK@levels.sait.edu.au (09/18/89)
In article <12988@uhnix1.uh.edu>, jsmng@csune.cs.uh.edu writes: > I used the following program trying to read in 5 numbers interactively. > As a test, I typed in 1.0 1.0 1.0 1.0 1.0 as input but the output is > about 0.47 (approximately I forget the exact value but it is far from 1). > When I changed the type of vec[] to be float. Everything is fine. > Does anyone have any experience with this? According to K&R, %f for > double should be allowed in scanf. We are running SunOS 4.0.1 and > I think our C compiler follows the K&R standards (as opposed to > ANSI). > > #include <stdio.h> > #define V 5 > main() > { > double vec[V]; > int i; > for (i=0;i<V;i++) > scanf("%f ",&vec[i]); > for (i=0;i<V;i++) > printf("%f ",vec[i]); > } You must use: scanf("%lf", &var) for doubles (long floats). Ray
jsmng@csuna.cs.uh.edu (09/19/89)
I posted a question about scanf last week. I could not scanf a double variable using %f. Thanks to many readers' contribution (There were too many to list out all names). My mistake was not reading my old K&R edition careful enough. Using %lf and %F would have worked. Printf accepts %f for float and double but not scanf. Mainly because the float parameters passed by value to printf will be converted to double. Scanf is passed by reference, so I have to do it right. James
pmaniac@walt.cc.utexas.edu (Noah Friedman) (09/20/89)
In article <12988@uhnix1.uh.edu> jsmng@csune.cs.uh.edu () writes: >I used the following program trying to read in 5 numbers interactively. >As a test, I typed in 1.0 1.0 1.0 1.0 1.0 as input but the output is >about 0.47 (approximately I forget the exact value but it is far from 1). >When I changed the type of vec[] to be float. Everything is fine. >Does anyone have any experience with this? According to K&R, %f for >double should be allowed in scanf. We are running SunOS 4.0.1 and > double vec[V]; > int i; > for (i=0;i<V;i++) > scanf("%f ",&vec[i]); I tried your program on a Sun2/50 (running UNIX system V) and got the same results. I know from past experience that the same thing happens on my IBM PC (using Turbo-C, which is supports ANSI C). My suggestion is to use "%lf" and don't worry about it. Noah Friedman pmaniac@walt.cc.utexas.edu
shankar@hpclscu.HP.COM (Shankar Unni) (09/20/89)
> I posted a question about scanf last week. I could not scanf a double > careful enough. Using %lf and %F would have worked. Printf accepts %f ^^^ NO. Don't use "%F". It was a non-standard extension to K&R at best, and will not work at all for ANSI C (since %F is exactly equivalent to %f). Stick to %lf. ----- Shankar Unni E-Mail: Hewlett-Packard California Language Lab. Internet: shankar@hpda.hp.com Phone : (408) 447-5797 UUCP: ...!hplabs!hpda!shankar