Multi-character character constants

utzoo!decvax!ittvax!tpdcvax!bobvan utzoo!decvax!ittvax!tpdcvax!bobvan
Wed Nov 10 13:53:57 AEST 1982


Alan Watt's recent comment about V6 C reminded me of a difficult time I once
had converting a V6 C program to run on a VAX.  The program interpreted two
letter commands and used a switch statement to decode them:

	switch (getchar()|(getchar()<<8)) {
		case 'ba':	/* ab command */
		case 'qw':	/* wq command */
		...
	}

(Disclaimer:  I do NOT suggest that you write code this way!)
(The calls on getchar() couldn't have been coded this way as the
compiler is free to execute them in either order.)

I am surprised to find that PCC (4.1bsd) will accept FOUR character
"character" constants.  Going for five finally gives the message "too
many characters in character constant".  Lint will complain about any
character constant more than one character long, but ONLY if you give
the -p (portability) option.  Try this program on your machine:

	main()
	{
		unsigned long c;

		c = 'dcba';
		while (c > 0) {
			putchar(c % 256);
			c >>= 8;
		}
	}

Section 2.4.3 of the "C Reference Manual" makes no mention of
multi-character character constants.  I am surprised to find a "modern"
compiler that still accepts them.

				Bob Van Valzah
				(...!decvax!ittvax!tpdcvax!bobvan)



More information about the Comp.lang.c mailing list