#Help with C !
6 messages in this thread
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?
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.
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….
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
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.
Glad you got it working. Perhaps something in the linked math library was
hosed.
-larry