A macro will not expand inside a string literal. Instead, you can use another macro to expand a macro into a string literal, and use string literal concatenation to create the desired string:
#define STR2(x) STR(x)
#define STR(x) #x
const char *cmd = "mode con lines=" STR2(WINY) " cols=" STR2(WINX);
system(cmd);
STR2
expands the provided argument (e.g. WINY
) into what it is defined to be and then passes it to STR
. STR
just uses the stringifying macro operator, and its result is a string literal. Adjacent string literals are concatenated into a single string by the compiler before the code is tokenized and compiled into object code.
If the macros are something more complex than simple numbers, then you need to manually construct a string. In C++, the easiest way is to use ostringstream
(from <sstream>
):
std::ostringstream oss;
oss << "mode con lines=" << WINY << " cols=" << WINX;
system(oss.str().c_str());