need help with a delcaration
Ken Thompson
thomps at gitpyr.UUCP
Tue Sep 9 23:41:38 AEST 1986
In article <1219 at drutx.UUCP>, qwerty at 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
More information about the Comp.lang.c
mailing list