Search code examples
cheaderconstantsstring-literals

shared c constants in a header


I want to share certain C string constants across multiple c files. The constants span multiple lines for readability:

const char *QUERY = "SELECT a,b,c "
                    "FROM table...";

Doing above gives redefinition error for QUERY. I don't want to use macro as backspace '\' will be required after every line. I could define these in separate c file and extern the variables in h file but I feel lazy to do that.

Is there any other way to achieve this in C?


Solution

  • In some .c file, write what you've written. In the appropriate .h file, write

    extern const char* QUERY; //just declaration
    

    Include the .h file wherever you need the constant

    No other good way :) HTH