need help with a delcaration

Richard Klappal rlk at chinet.UUCP
Sun Sep 14 04:26:23 AEST 1986

In article <2233 at gitpyr.UUCP> thomps at gitpyr.UUCP writes:
>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

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

More information about the Comp.lang.c mailing list