rhh88@ecs.soton.ac.uk (Heywood RH) (03/18/91)
A friend of mine has discovered something a little stange with the ansi c compiler. It seems that he could only get it to pass a float as a function param. if he used the new ansi syntax. If he used the old syntax it gave a compile error. Stange thing is that if he changed his float to a double it worked. Odd eh! I think this could count as a bug but I don't want to go round accusing people only to discover it my fault :-( Here is the example bit of code for you to try and compile to see :- void a_function( float ); void other_func( double ); void other_function( distance ) double distance; { printf("%f", distance ); } void a_function( a_number ) float a_number; { printf("%f", a_number ); } /* Change first line of this function to void a_function( float a_number ) to get it to compile. */ END I think that was how it went but I am doing this from memory. Hope this helps someone RIK ______________________________________________________ / \ / Richard Heywood (Rik) \ / rhh88@uk.ac.soton.ecs \ \ / \ The Spice must flow / \______________________________________________________/ There is no time like the present for postponing what you ought to be doing.
osmith@acorn.co.uk (Owen Smith) (03/19/91)
In article <7199@ecs.soton.ac.uk> rhh88@ecs.soton.ac.uk (Heywood RH) writes: >A friend of mine has discovered something a little stange with the ansi >c compiler. It seems that he could only get it to pass a float as a >function param. if he used the new ansi syntax. If he used the old >syntax it gave a compile error. Stange thing is that if he changed his >float to a double it worked. I think the reason is fairly simple. Remember the function argument type promotion rules in traditional C. Floats promote to double. Thus any functions should have their arguments declared as double not float, because by the time it gets to the function it will be a double. The compiler has probably spotted you declaring a parameter as float for a function with no prototype, and is telling you that this will not work. This is good. On other systems, you get junk parameters as the promoted argument takes 64 bits as a double but the function accesses it as 32 bits as a float. Bad news. If you have ever wondered why all the C runtime library floating point functions all take doubles as arguments, now you know. It used to be all you could pass. Function prototypes are wonderful things. Owen. The views expressed are my own and are not necessarily those of Acorn.