CTRL(x)

Michael Meissner meissner at dg_rtp.UUCP
Mon Nov 3 10:21:55 AEST 1986


In article <4880 at brl-smoke.ARPA> kb5mu at NOSC.ARPA writes:
>>But. . .is there a way to write an ANSI cpp version of
>>        #define CTRL(c)
>
>I don't have an ANSI cpp handy (does anybody?), so I can't test
>this, but how about using the ANSI invention of # for "string-izing":
>        #define CTRL(c) (#c[0] & 037)
>This should turn
>        CTRL(z)
>into
>        ("z"[0] & 037)
>which would get what you wanted, except that relatively stupid
>compilers might allocate storage for the string literal "z".

The problem with using string-izing, is that it is not a constant expression,
and can't be used in switch statements.  Many of the places CTRL(x) is
currently used ARE in switch statements.  Another approach would be to use
token pasting, instead of string-izing:

	enum _ctrl_chars {
		_ctrl_a = 1,
		_ctrl_b = 2,
			/* c, d, etc. */
		_ctrl_z = 26,
		_ctrl_A = 1,
		_ctrl_B = 2,
			/* C, D, etc. */
		_ctrl_Z = 26
	};

	#define CTRL(x)		(int)( _ctrl_ ## x)

The problem with the above is that people have also used CTRL([) to
represent the escape character.

	Michael Meissner, Data General
	...mcnc!rti-sel!dg_rtp!meissner



More information about the Comp.lang.c mailing list