Search code examples
cmacroscharwidechar

C Macro should expand as char* in one place and wchar* in another place with L character


I am looking for help with writing a C macro that expands into char* macro in one place and wchar_t* macro in another place.

Eg:

#define MACRO_STRING "Macro String"

to atomic section with function calls:

{
    function1(L"Macro String");
    function2("Macro String");
}

function1 always accepts wide characters wchar_t *, while function2 always accepts char *.

In most places in my code I am calling function1 immediately followed by function2. Throughout my code, the string "Macro String" is a constant. But one function takes input as wchar* while the other takes input as char*.

Need help in expanding the macro differently in the two functions


Solution

  • A quick and easy solution, with no extra macros:

    • Use MACRO_STRING directly when you need char *.
    • Use L"" MACRO_STRING when you need wchar_t *.