BartC said:
For short string matching I've used char literals such as 'abcd' or
'abcdefgh'. These can't be compared quite as quickly as compact enum codes,
but it's faster than string matching. Also you don't need to maintain a set
of enums for each function you want to use keyword parameters with; you just
use the parameter name (or a version of it to fit within the 4- or
8-character limit).
But, I think C doesn't like you using multi-char constants like this (trying
it now, gcc doesn't even like more than 4 characters), which limits the use
of this method.
The standard doesn't even guarantee that 'ab' and 'ba' have distinct
values; it only says that their values are implementation-defined
(and of type int). On the other hand, they'll be distinct in any
sane implementation, and assuming that they'll be distinct doesn't
particularly bother me. (Assuming they have some particular value
would be unwise if your code is supposed to be at all portable.)
gcc warns about all multi-character character constants by default
(a reasonable warning IMHO, but not required by the standard).
It issues a different warning for multi-character constants of more
than 4 characters (where sizeof (int) == 4) -- but that warning is
also not required, or even suggested, by the standard.
For this program:
#include <stdio.h>
int main(void) {
printf("'a' = 0x%x\n", (unsigned)'a');
printf("'ab' = 0x%x\n", (unsigned)'ab');
printf("'abc' = 0x%x\n", (unsigned)'abc');
printf("'abcd' = 0x%x\n", (unsigned)'abcd');
printf("'abcde' = 0x%x\n", (unsigned)'abcde');
}
gcc produces the following warnings:
c.c: In function 'main':
c.c:4:42: warning: multi-character character constant [-Wmultichar]
c.c:5:42: warning: multi-character character constant [-Wmultichar]
c.c:6:42: warning: multi-character character constant [-Wmultichar]
c.c:7:42: warning: character constant too long for its type [enabled by
default]
and this output:
'a' = 0x61
'ab' = 0x6162
'abc' = 0x616263
'abcd' = 0x61626364
'abcde' = 0x62636465
The program is perfectly legal, and any conforming implementation must
accept it. Its output is implementation-defined.
I wouldn't use multi-character constants myself other than to find out
what a compiler does with them.
I've seen them most often in code written by inexperienced programmers
who haven't yet learned the difference between single and double quotes.