[comp.sys.ibm.pc] Programmer error

jfbruno@rodan.acs.syr.edu (John F. Bruno) (02/20/90)

In article <204@sdscal.UUCP> keith@sdscal.UUCP (Keith Jones) writes:
 [ message asking for bug details omitted ]
 >
 >Well, I didn't quite believe it, but I made this test file and sure enough,
 >the printf doesn't print out anything that even looks like it approximates
 >PI.
 >
 >----- to cut ----- or not to cut -----
 >main()
 >{
 >    float f;
 >    char *string = "3.1415926";
 >
 >    f = atof( string );
 >    printf( "pi = %f\n", f );
 >}
 >----- be it here ----- or there -----
 >
 >The output to this program was:
 >
 >D:\ -> test
 >
 >-8209.00000
 >
 >and I compiled it with:
 >
 >D:\ -> tcc test
 >
 >I'm not particularly worried about this bug, but I figured I might as well
 >post the program.
 
atof() takes a character pointer as its argument and returns a float, in C,
anytime you don't explicitly declare what a function returns, it is assumed
to be an integer (I know, I would prefer a warning also), that's what 
happened in this case, because you didn't #include <math.h> I got the same
result with my test program, but when you include the prototype for atof(),
it works perfectly. If you look up atof() in the Turbo-C reference guide, it
says "prototype in math.h,stdlib.h"

---jb

schaut@cat9.cs.wisc.edu (Rick Schaut) (02/20/90)

In article <2126@rodan.acs.syr.edu> jfbruno@rodan.acs.syr.edu (John F. Bruno) writes:
| 
| In article <204@sdscal.UUCP> keith@sdscal.UUCP (Keith Jones) writes:
|  [A C program that printed a value of Pi after a call to atof().]
|  
| atof() takes a character pointer as its argument and returns a float, in C,
| anytime you don't explicitly declare what a function returns, it is assumed
| to be an integer (I know, I would prefer a warning also), that's what 
			    ^^^^^^^^^^^^^^^^^^^^^^^^
| happened in this case, because you didn't #include <math.h> I got the same
| result with my test program, but when you include the prototype for atof(),
| it works perfectly. If you look up atof() in the Turbo-C reference guide, it
| says "prototype in math.h,stdlib.h"

Um, there _is_ a command-line switch that turns such warnings on.  If you
don't like having to type the switch every time, look-up turboc.cfg in
the User's Guide.

Whenever I get a question about a bug, the first two questions I ask are,
"did you turn on _all_ warnings before compiling", and "did your sample
code compile without any such warnings".  If the answer to _either_ question
is no, I send the person back to try again.

--
Rick (schaut@garfield.cs.wisc.edu)

Peace and Prejudice Don't Mix! (unknown add copy)