shared c constants in a header

Manish picture Manish · Mar 31, 2011 · Viewed 33.1k times · Source

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?

Answer

Armen Tsirunyan picture Armen Tsirunyan · Mar 31, 2011

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