🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: net.lang.c
Article #9308

Re: need help with a delcaration

#9308
From: karl
Date: Fri, 12 Sep 1986 15:23
35 lines
2292 bytes
BJORNDAS%CLARGRA@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@haddock.isc.com), The Walking Lint
Technical Details
Message-ID: <86900053@haddock>
Path: arc1-ca.pugleaf.net!utzoo!mnetor!seismo!think!ima!haddock!karl
References: <3594@brl-smoke.ARPA>

Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: arc1-ca.pugleaf.net!utzoo!mnetor!seismo!think!ima!haddock!karl
From: karl@haddock
Newsgroups: net.lang.c
Subject: Re: need help with a delcaration
Message-ID: <86900053@haddock>
Date: Fri, 12-Sep-86 11:23:00 EDT
Article-I.D.: haddock.86900053
Posted: Fri Sep 12 11:23:00 1986
Date-Received: Sun, 14-Sep-86 20:14:28 EDT
References: <3594@brl-smoke.ARPA>
Lines: 35
Nf-ID: #R:brl-smoke.ARPA:3594:haddock:86900053:000:1735
Nf-From: haddock!karl    Sep 12 11:23:00 1986