two chars at once...

Karl Heuer karl at haddock.ima.isc.com
Tue Sep 19 10:10:31 AEST 1989


In article <11057 at smoke.BRL.MIL> gwyn at brl.arpa (Doug Gwyn) writes:
>In article <QZ3bmsW00WB4E5BEob at andrew.cmu.edu> sr16+ at andrew.cmu.edu (Seth Benjamin Rothenberg) writes:
>>One of the nasty things it does is treat 2 characters
>>as a single 2-byte integer.
>>	if  "ax" = mystr    ->  if mystr[1]=>C1D9  (or something like that)
>
>The direct equivalent in C would be to use multi-character character
>constants such as 'AX', which [contain both characters in an implementation-
>dependent manner].  Using 2-character strings would be more portable

(Terminology: since X3J11 has already claimed the words "multibyte character"%
and "wide character"&, and neither one of them refers to the construct above,
I have taken to calling them "siamese character constants".)

If the inefficiency of using two-character strings is a problem, and you don't
want to rely on the properties (or even the existence) of siamese character
constants, you can replace them with a macro:
	#define two(a,b) (((a)<<CHAR_BIT)|(b))
	if (two('a','x') == mystr) ...
(It doesn't matter whether two() is defined as above or the other way around,
as long as it's consistent.)

Karl W. Z. Heuer (ima!haddock!karl or karl at haddock.isc.com), The Walking Lint



More information about the Comp.lang.c mailing list