Article View: net.lang.c
Article #9305Re: need help with a delcaration
From: Richard Klappal
Date: Sat, 13 Sep 1986 18:26
Date: Sat, 13 Sep 1986 18:26
54 lines
2739 bytes
2739 bytes
In article <2233@gitpyr.UUCP> thomps@gitpyr.UUCP writes:
>
>
>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
Balderdash!!!!!
K&R declare all chars as ints so that routines that return EOF will
work correctly regardless of whether chars are signed or unsigned
in your hardware/software combination.
Your compiler is broken if it will not work as shown in the example,
and if you define the arg as an int in the subroutine, you \'should\'
cast the char arg to an int in the calling sequence. The automatic
promotion of char to integer only means that everything will probably
work if you don't do the casting.
--
---
UUCP: ..!ihnp4!chinet!uklpl!rlk || MCIMail: rklappal || Compuserve: 74106,1021
..!ihnp4!ihu1h!rlk
---
Technical Details
Message-ID:
<547@chinet.UUCP>Path:
arc1-ca.pugleaf.net!utzoo!mnetor!seismo!caip!clyde!cbatt!ihnp4!chinet!rlkReferences:
<3594@brl-smoke.ARPA> <1219@drutx.UUCP> <2233@gitpyr.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!caip!clyde!cbatt!ihnp4!chinet!rlk From: rlk@chinet.UUCP (Richard Klappal) Newsgroups: net.lang.c Subject: Re: need help with a delcaration Message-ID: <547@chinet.UUCP> Date: Sat, 13-Sep-86 14:26:23 EDT Article-I.D.: chinet.547 Posted: Sat Sep 13 14:26:23 1986 Date-Received: Sun, 14-Sep-86 03:45:32 EDT References: <3594@brl-smoke.ARPA> <1219@drutx.UUCP> <2233@gitpyr.UUCP> Reply-To: rlk@chinet.UUCP (Richard Klappal) Organization: chi-net, Public Access UN*X, Chicago IL Lines: 54