The bug in Turbo C 2.0

Mike Percy grimlok at hubcap.clemson.edu
Sat Mar 17 01:31:08 AEST 1990


>From article <1990Mar14.174620.11101 at metro.ucc.su.OZ.AU>, by chii at ee.su.oz.au (Liang Chii			):
> Hi, net reader,
> 
> I found a bug in Turbo C, please reconfirm if it is truely a bug.
> ======= cut here =======
> #include <stdio.h>
> #include <alloc.h>
> 
> main() {
> float far *temp, fix;
> int i;
> 
> temp = farmalloc(40);
> for(i = 0; i< 5; i++)
> scanf("%f", temp+i);
> }
> ===== end here ======
> 
> The above code does not work but with an error message :
> "floating point formats not linked"
> 
> But, if we put another statement at the end of the program :
> 
> scanf("%f", &fix);
> 
> Now, re-compiler and link the code.  It works ok.
> It is very obvious that the code "scanf("%f", &fix);" should not
> affect the about program like this.  I believe there is a bug
> between farmalloc(farcalloc) and scanf("%f",...).
> 
 
This happens frequently with TC.  In my experience it is not what I would
exactly a bug, more like the user not knowing what the compiler is doing
and why it is doing it.  TurboC uses a floating point emulation library
if you do not have a co-processor.  Printf() drags this in even if you
do not do floating operations (unless you specify -f-).  If you do any
operations on a float  or double value, you get this brought in.
Scanf() does not drag the floating point stuff with it, because scanf
simply works on addresses and strings of characters (i.e. no float
operations), and puts its results in the memory you tell it to.  Its up to
the programmer to properly size and treat that memory!

The reason your 
code didn't work properly in the first case was because you haven't done
any operations on (technically) floats or doubles.  What you did was 
allocate a pointer to something that is sized and can safely be treated
as a float.  You then pointed this at 40 bytes of memory, which you want 
to treat as a number of floats to be read by scanf, but which you could just have easily wanted to be a bunch of ints or structs.  The fix you propose     
works not beacuse of some glitch in scanf, but because now you have performed 
an operation (address of) something which the compiler _knows_ is a float,
and not just some memory which happens to be treated like a float. 
A fix which is use more often is to add a line in main() like
(void) log(1.0);

Your code now includes something the compiler recognizes as indicative of
requiring the floating point libraries, and wham, bam, you're set.

Good luck.



More information about the Comp.lang.c mailing list