OMGtechy OMGtechy - 2 months ago 10
C Question

In a GNU C macro envSet(name), what does (void) "" name mean?

I came across this syntax today and couldn't work out what it meant:

// Uses the GNU C statement expression extension
#define envSet(name) ({ \
static int initialised; \
static bool set; \
(void) "" name; \
if (!initialised || !g_cacheEnv) { \
const char *value = getenv(name); \
set = value != NULL; \
initialised = true; \
} \
set; \
})


The specific line I cannot understand is:

(void) "" name; \


Could somebody please shed some light on this?

Answer

It looks like a way to statically ensure that name is a string literal and not some other type.

If you do (void)"" "hello"; then it is a valid C expression.

But if you do something like (void)"" 1; then you get a syntax error.