Mixing char and int types as parameters in portable (summary).

Guido van Rossum guido at mcvax.UUCP
Mon Mar 19 21:36:46 AEST 1984


A week ago, I asked in this group whether the following is portable:
	foo(c) char c; { ... }
	bar(i) int i; { ... }
	main() { foo(100); bar(' '); }

Thanks to all of you who took the time to reply.  The general opinion is:
Mixing chars and ints as parameter types is portable, because (section 6.1
of K&R) "A character . . . may be used wherever an integer may be used.  In
all cases the value is converted to an integer."
  The implication is that in the call bar(' '), the space is converted to an
integer before parameter passing.  The same argument shows that foo(c) must
expect that its argument, even when stated as a character (e.g. foo(' '))
has been converted to an integer, so foo((int)' ') is portable too.
  (My own opinion is that this stems at least partly from the fact that on the
PDP-11 the stack pointer must be an even address, so that characters passed
as parameters had to be aligned anyway.)

Several people pointed out that my example (which was chosen rather
careless) is NOT portable because the conversion between char and int is
not defined, and there may be machines where 100 does not fit in a
character; there is also the annying problem whether characters are considered
signed or not.  However, this is not what bothered me (I don't use foo(100),
just foo(i) where i is returned by getchar() and not EOF).

>From the results of this questionnaire, I conclude that it is best not to use
simple char variables or parameters, but always declare them as int.  This
has the additional benefit of allowing them to be put in registers, while
the Berkely C compiler won't put characters in registers (is this still
true?).  Any comments?

Guido (soon to be included in the SRI Phone Book) van Rossum,
	CWI, Amsterdam
	guido at mcvax



More information about the Comp.lang.c mailing list