`char' parameters

Bill Gibbons bgibbons at Apple.COM
Fri Sep 2 02:38:55 AEST 1988


In article <1616 at se-sd.sandiego.ncr.com> rns at se-sd.sandiego.NCR.COM (Rick Schubert) writes:
>I believe I have found a bug in a C compiler I am using ...
>If I define a function `f' as follows:
>	f(c)
>	char	c;
>	{
>		...
>	}
>
>the compiler in question gives a warning that type type of `c' is
>being changed to `int'.
> ...

In section 3.7.1 of the draft standard, it says:
   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.

This is known as _narrowing_.  It is very important, for exactly the reason
you point out: if the parameter is not narrowed, and you take its address,
the pointer is to a different type than expected.

Narrowing is very easy for a compiler to do: for CHAR and SHORT, it just
adjusts the stack offset at which it thinks the value was passed, and changes
the type.  (On PDP11 etc, it doesnt even change the offset.)  Floating-point
is a little harder on most machines; it needs an explicit conversion.

This caused me a lot of effort last fall, when I was porting a UNIX application
to IDRIS running on an Atari box.  The Whitesmiths compiler did not do
narrowing, and I had to modify the code (with a tool) to add explicit
narrowing.  Without a tool to systematically change the code, it would have
been hopeless.  (After I got it working and it showed at Comdex, Atari
cancelled the product.  Fortunately, someone else was paying :-)

Bill Gibbons
(contractor)
bgibbons at apple.com



More information about the Comp.lang.c mailing list