Possible C compiler bug?

David B.Anderson davea at quasar.wpd.sgi.com
Tue Sep 11 02:33:35 AEST 1990


In article <1379 at cs.nps.navy.mil>, spl at cs.nps.navy.mil (Steve Lamont) writes:
> I'm having a problem with a piece of C code on a 4D/70GT under 3.2.  I have
> declared a variable as float (actually Coord but they're effectively the
[stuff deleted]
> The following test code shows the problem:
[stuff deleted]
>  
> foo( a, b )
>   float *a;
>   float b;
>   {
[stuff deleted]
>     bar( &b, b );
>     return;
>   }
[stuff deleted]
> I've looked through K&R (2nd Edition) and can't find anything that
> specifically bears upon this problem in the sections on type coersion.  Am I
> doing something wrong?

Since this re-appears periodically, I'll respond on the net.  Perhaps
this topic should be in Frequently Asked Questions on comp.lang.c.

K&R1: b is a double, not a float.

K&R 1, page 205:   ``formal parameters declared float have their
declaration adjusted to read double.''      This means the type-rewriting
that ccom is doing (the float b is taken internally as if it were written
double b) is justified, since 3.2 and 3.3 cc are not ANSI C (and this
function is not written in prototype form).

ANSI C is quite different:

ANSI C, 3.7.1: ``On entry to the function the value of each argument
expression shall be converted to the type of its corresponding parameter,
as if by assignment to the parameter.'' So the code would work as written
in ANSI C.  There's a discussion in the ANSI C Rationale, section 3.7.1.

Finally, K&R address the change specifically:

K&R2, Sec A 10.1, page 226: ``...the first edition specified
that the declarations of float parameters were adjusted to read double.
The difference becomes noticeable when a pointer to a parameter is gnnerated
within a function.''

Summary:  There is no bug in cc here.  K&R1 and ANSI C differ.

Regards,
[ David B. Anderson  Silicon Graphics  (415)335-1548  davea at sgi.com ]

PS: Many thanks to Doug Gwyn, Henry Spencer, and a few others for their
answers on this and other C questions.  Responsibility for any mistakes in
this posting is mine, of course, not theirs......



More information about the Comp.sys.sgi mailing list