Concatenating string and char literals in C

In my code, I define some compiler constants, such as:

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

(it can be characters, 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 I get is that I am mixing types and I get warnings from the compiler.

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

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

+4
source share

Source: https://habr.com/ru/post/1652618/


All Articles