CompuServe Thread

#Help with C !

6 messages in this thread
#23556From: John ParkerMay 16, 1992 9:04 PM
As a second attempt at C programming I tried the following: #include <stdio.h> main () { float real; printf("Enter a float number: "); scanf("%f", &real); printf("Float: %f\n", real); } The result is: Enter a float number: 3.1 Float: 2.137500 Why in the world does this happen? This does not occur when an integer is used (changing %f to %d) and when I set real=constant the correct constant is output. I presume there is something wrong with the way I use scanf and floats. I am using SAS C with an A3000. Would a compiler option do this?
#23563From: John DraperMay 17, 1992 12:53 AM
I tried your code, and it works just fine… result is: Enter a float number: 3.1 Float: 3.100000 The first time I tried it, I got a BLink errror: Undefined symbols First Referenced _CXV45 File 'test.o' I then compiled with the command line: lc -Lm test.c And it worked fine.
#23579From: John ParkerMay 17, 1992 9:30 PM
This is quite bizarre. I think I'll go back to fortran. Trying a series of numbers gave the following: Input Output 0. 0.0 1. 1.875 2. 2.0 3. 2.125 4. 2.25 5. 2.3125 6. 2.375 10. 2.5625 15. 2.71875 20. 2.8125 I have been using lc -Lm. I'll presume my hardware is fine and start digging around in the SAS manuals again for some clues. Maybe I'll wipe it all out and re-install….
#23595From: Don Curtis/SYSOPMay 18, 1992 1:19 AM
John, Either make real a double (rather than a float) or use the fabs() macro in math.h (see page L2 in the SAS/C manual). Don
#23582From: John ParkerMay 17, 1992 10:11 PM
I just got done re-installing SAS and now everything is fine. I wish I knew what happened, or what setting I messed up to cause that to happen.
#23594From: John DraperMay 18, 1992 1:09 AM
Glad you got it working. Perhaps something in the linked math library was hosed. -larry