need help with a delcaration

karl at haddock karl at haddock
Sat Sep 13 01:23:00 AEST 1986


BJORNDAS%CLARGRA at WISCVM.WISC.EDU (Sterling Bjorndahl) writes:
>>foo(ch)
>>char ch;  /* Or should this be "int ch;" because it gets promoted? */

haddock!karl (Karl Heuer) replies:     | drutx!qwerty (Brian Jones) replies:
>[int is better;] one should never     | >char ch is the correct declaration.
>declare formal arguments of [type     | >The compiler should handle pulling
>char, short, float, or array].  [But] | >the character portion.  Declaring
>your compiler is broken.              | >it 'int' is asking for trouble.

Well, now that's cleared up.  :-)

Actually, I think I may have spoken too quickly.  It *is* misleading to
declare a float or array formal parameter -- the compiler silently converts
it to a double or pointer declaration -- but char and short aren't affected
the same way, at least not here (SVR2 vax).

example:
foo(ch, sh, fl, ar) char ch; short sh; float fl; int ar[10]; { ... }
sizeof(fl)==sizeof(double) not sizeof(float); &fl is "double *" not "float *"
sizeof(ar)==sizeof(int *) not sizeof(int[10]); &ar is "int **" not "int(*)[]"
but, sizeof(ch)==sizeof(char) and &ch is "char *".

So maybe it is safe; I can't find a definitive statement in K&R.

However, I disagree that "declaring it 'int' is asking for trouble".  It may
or may not undergo sign extension, but that's true of any use of char.  The
actual argument *was* converted from char to int by the caller, so the value
of the (int) formal argument is predictable.  If it's declared "char" it has
a predictable value, but I'm not convinced it has a well-defined type.  In
any case (unless you were doing more than your posting implied) you have a
broken compiler.

Karl W. Z. Heuer (ima!haddock!karl; karl at haddock.isc.com), The Walking Lint



More information about the Comp.lang.c mailing list