4

In my code I define some compiler constants such as the following:

#define D_CR  '\x10'    // New line
#define D_LF  '\x13'    // New paragraph
#define D_EOS '\xFF'    // End of string

(these could be chars, ints, whatever ... )

And I want to use them in two ways, one in a string literal, and secondly in switch statements.

unsigned char dialogString[] = 
    "LOREM IMSUM" D_CR
    "DOLAR SIT A MET" D_EOS;

switch (dialogString[i]) {
    case D_CR: /* ... */ break;
    case D_LF: /* ... */ break;
    case D_EOS: /* ... */ break;
    default: printf(dialogString[i]); break;
}

The problem that I'm getting is that I'm mixing types and I'm getting compiler warnings.

dialogString.c(5) parse error: token -> ''\x10'' ; column 11

Is there any way that I can get this to work for both scenarios?

William Pursell
  • 204,365
  • 48
  • 270
  • 300
bashaus
  • 1,614
  • 1
  • 17
  • 33
  • 2
    good question, but I'm afraid the preprocessor limitations won't allow that. – Jean-François Fabre Aug 26 '16 at 15:16
  • no, not `'\x13'`. It's either `13` or `'\xd'` or `'\x0d'`, maybe even `'\n'` if you're worried about character encodings. – pmg Aug 26 '16 at 15:17
  • @pmg parse error: token -> ''\x0d' ; column 11 – bashaus Aug 26 '16 at 15:20
  • @Jean-FrançoisFabre does this mean i should create two compile-time constants to sort this (one char and one string)? – bashaus Aug 26 '16 at 15:21
  • See [code running at codepad](http://codepad.org/qLbgyhGT). – pmg Aug 26 '16 at 15:25
  • @pmg: are you able to post an answer (I don't have access at codepad from here)? – Jean-François Fabre Aug 26 '16 at 15:32
  • The codepad entry is just about `\x13`, not an answer to your actual question – pmg Aug 26 '16 at 15:51
  • 1
    I'm unclear how the codepad code addresses the problem. It just seems to define the original constants, then confirm that they have been defined. No string literals involved. – Tommy Aug 26 '16 at 15:52
  • Not sure if a previous answer of mine to another question suits you. http://stackoverflow.com/questions/1595544/c-macro-turn-a-number-into-a-string – pmg Aug 26 '16 at 15:53

0 Answers0