Multiple character "character constants"
Steve Summit
stevesu at bronze.UUCP
Fri Oct 7 04:19:13 AEST 1983
I'm sure I won't be the only one to post a followup on this, but
I haven't seen any yet, so here goes.
Multiple-character character constants are a horrible idea.
Whenever I see one, I have no idea whether or not it's a typo.
If it's a typo, I don't know if the extra characters are
extraneous or if a string was intended. If it's not a typo, I
don't know if the programmer knew what he was doing, or if the
program has a chance of working on my machine.
Character constants are explicitly defined to be a single
character. (Kernighan and Ritchie, page 35, and again on page
180.) On the other hand, Appendix A, under "portability
considerations" notes (page 212) that "Since character constants
are really objects of type _i_n_t, multi-character character
constants are permitted. The specific implementation is very
machine dependent, however, because the order in which characters
are assigned to a word varies from one machine to another."
I didn't really understand why multi-character constants would be
useful in the example given in the original article. (I admit
that I didn't try very hard.) I would never use a
machine-dependent "feature" like that. Portability is a very
important issue. I jump back and forth between vaxes and 11's,
and I like to take my programs with me. I only write machine
dependent code when I have to (when I'm using assembly language
to do something I can't do in C, or writing code that deals with
object file format.) If you really want to do a switch on
multiple-character sequences, use a bunch of strcmp's or a hash
function to convert a genuine string to a genuine integer.
Steve Summit
tektronix!tekmdp!stevesu
More information about the Comp.lang.c
mailing list