C/C++ How to expand a macro parameter into text between quotes

c, c++, macros

Solution

Don't put `#fmt` in the quotes. Just use string literal concatenation to join the two literals.

#define LOG(fmt, ...) message("%s %s(): " fmt, __FILE__, __func__, __VA_ARGS__);

Problem

Here is what I have (message() is a specialized logging function from a third party library): ``` #define LOG(fmt, ...) message("%s %s(): #fmt", __FILE__, __func__, __VA_ARGS__); ``` So I want to be able to do things like: ``` LOG("Hello world") LOG("Count = %d", count) ``` And have it expand to: ``` message("%s %s(): Hello world", __FILE__, __func__); message("%s %s(): Count = %d", __FILE__, __func__, count); ``` But the #fmt thing is not working. It does not evaluate to the macro argument and prints as "#fmt". Is it possible to do what I'm trying to do?

Original source

Related problems