Article View: net.lang.c
Article #9284Re: need help with a delcaration
From: Ken Thompson
Date: Tue, 09 Sep 1986 13:41
Date: Tue, 09 Sep 1986 13:41
35 lines
1956 bytes
1956 bytes
In article <1219@drutx.UUCP>, qwerty@drutx.UUCP (Brian Jones) writes:
> In the sequence:
>
> main()
> {
> char c = 'w';
> foo(c)
> }
>
> foo(ch)
> char ch;
>
> char ch is the correct declaration. The compiler/code generator should
> handle pulling the character portion of the promoted variable off the
> stack correctly. Declaring it 'int' is asking for trouble.
>
If I understand what Kernighan and Ritchie say in their book, then ch
is automatically converted to an int when foo is called because a function
argument is an expression and a character is always converted to an int in
an expression. See K&R page 41 - 42.
I know that some compilers take care of this and allow you to
still declare ch as a char. However, I note that K&R always declare
ch as an int in their examples. I would suppose that the compiler being
used requires the int declaration. Since the conversion occurs by definition
of the language, there is no danger in declaring it an int and this is
commonly done in most C code. Declare it as an int and go to it. It also
sounds like the asker of the original question should get hold of K&R and
learn about the relationship between char and int in C.
--
Ken Thompson
Georgia Tech Research Institute
Georgia Insitute of Technology, Atlanta Georgia, 30332
...!{akgua,allegra,amd,hplabs,ihnp4,seismo,ut-ngp}!gatech!gitpyr!thomps
Technical Details
Message-ID:
<2233@gitpyr.UUCP>Path:
arc1-ca.pugleaf.net!utzoo!mnetor!seismo!gatech!gitpyr!thompsReferences:
<3594@brl-smoke.ARPA> <1219@drutx.UUCP>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!gatech!gitpyr!thomps From: thomps@gitpyr.UUCP (Ken Thompson) Newsgroups: net.lang.c Subject: Re: need help with a delcaration Message-ID: <2233@gitpyr.UUCP> Date: Tue, 9-Sep-86 09:41:38 EDT Article-I.D.: gitpyr.2233 Posted: Tue Sep 9 09:41:38 1986 Date-Received: Tue, 9-Sep-86 23:56:32 EDT References: <3594@brl-smoke.ARPA> <1219@drutx.UUCP> Organization: Georgia Institute of Technology Lines: 35 Summary: int is correct I think