Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!abvax!iccgcc!browns From: browns@iccgcc.decnet.ab.com (Stan Brown, Oak Road Systems) Newsgroups: comp.lang.c Subject: Re: Nasty bug Message-ID: <707.26dd12c1@iccgcc.decnet.ab.com> Date: 30 Aug 90 18:21:03 GMT References: <0093BF08.7F3834E0@rigel.efd.lth.se> <2117@krafla.rhi.hi.is> Lines: 37 In article <2117@krafla.rhi.hi.is>, pierre@rhi.hi.is (Kjartan Pierre Emilsson) writes: > From article <0093BF08.7F3834E0@rigel.efd.lth.se>, by e89hse@rigel.efd.lth.se: >> >> [Code deleted] >> prnval(s,f) >> char *s; >> float f; >> >> And it didn't work. Why? The answer is that the parameter f is a >> double, not a float since all floats are converted to double when they are >> passed as arguments to functions.Therefore &f is a ptr to double rather than >> a float. >> >> Henrik Sandell > > I don't know what other think, but as a relatively experienced C programmer, I > find this bug very counter-intuitive, [...] This isn't a bug--or at least it's not a compiler bug; it's the way the language is designed to work. Page 184, sec 6.2 of K&R 1: "whenever a float appears in an expression it is lengthened to double. ..." But I think the real moral of the story is, always use prototypes. The code shown above doesn't allow the compiler to do type checking. If the function were defined as int prnval(char *s, float f) { ... then the compiler is not just able but _required_ to type-check the arguments. (I think the original post had the function in the same source file and preceding the code of the function that called it. If that's not the case, then the prototype declaration would be need to be in scope.) Stan Brown, Oak Road Systems, Cleveland, Ohio, U.S.A. (216) 371-0043 The opinions expressed are mine. Mine alone! Nobody else is responsible for them or even endorses them--except my cat Dexter, and he signed the power of attorney only under my threat to cut off his Cat Chow!