I have some static variables (say, var1 and var2) declared in two different files. The variables have same name in both files. Some variables (say var1) are not initialized in their declaration and some are (var2), like following.
file1.h
static bool var1;
static bool var2 = false;
file2.h
static bool var1;
static bool var2 = false;
According to my understanding, static variables are only restricted to the c files(or h files) they're declared in, so I should be safe having same variable names in multiple header files. But when I compile code, I get error "redefinition of var2", only for the variables that have been initialized.
- Why this error occurs only for var2 ?
- Is my implementation alright ?
Edit: since some mentioned to use extern keyword, I'd like to clarify that both var1 and var2 are supposed to have different values in different c files, and should only be restricted to their respective files,