Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83 (MC840302); site mcvax.UUCP Path: utzoo!watmath!clyde!akgua!mcnc!philabs!mcvax!guido From: guido@mcvax.UUCP (Guido van Rossum) Newsgroups: net.lang.c Subject: Mixing char and int types as parameters in portable (summary). Message-ID: <5743@mcvax.UUCP> Date: Mon, 19-Mar-84 06:36:46 EST Article-I.D.: mcvax.5743 Posted: Mon Mar 19 06:36:46 1984 Date-Received: Tue, 20-Mar-84 06:25:17 EST Organization: CWI, Amsterdam Lines: 33 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@mcvax