I have a header file A.h in which i define a const int ID = 4;. I have included this header file in to C files A.c and main.c. I have used header guards #ifndef A_H #define A_H etc. But I get the error multiple definition of ID when I try to compile the code.
I read somewhere that this can, in most cases, be avoided by using #pragma once but I still get the error.
My question is that how can I define variables in C? Should I have to move definition of ID to C file but then I have to declare it in every file I use? Or using extern the only way in this situation?
I have a header file A.h in which i define a const int ID
Share
Yes, using
externis the only solution.pragmaor include guards guard against multiple inclusion in the same translation unit, this is a multiple definition error.