shared c constants in a header

前端 未结 4 1234
庸人自扰
庸人自扰 2021-01-01 10:23

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 \"
         


        
4条回答
  •  萌比男神i
    2021-01-01 10:51

    You could use static consts, to all intents and purposes your effect will be achieved.

    myext.h:

    #ifndef _MYEXT_H
    #define _MYEXT_H
    static const int myx = 245;
    static const unsigned long int myy = 45678;
    static const double myz = 3.14;
    #endif
    

    myfunc.h:

    #ifndef MYFUNC_H
    #define MYFUNC_H
    void myfunc(void);
    #endif
    

    myfunc.c:

    #include "myext.h"
    #include "myfunc.h"
    #include 
    
    void myfunc(void)
    {
        printf("%d\t%lu\t%f\n", myx, myy, myz);
    }
    

    myext.c:

    #include "myext.h"
    #include "myfunc.h"
    #include 
    
    int main()
    {
        printf("%d\t%lu\t%f\n", myx, myy, myz);
        myfunc();
        return 0;
    }
    

提交回复
热议问题