Does it make any difference in C99 when one writes const int x = 1; vs. static const int x = 1; in a header (*.h) file?
Does it make any difference in C99 when one writes const int x =
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
As cdarke said, it makes a difference.
const int x = 1;creates a for the linker visible symbol, for each module including your h-file.The linker should stop then with an error, as there are multiple (visible) defines of the same symbol.
static const int x = 1;creates a variable but no linker symbol, for each module including your h-file.The linker could link the code, but as you created multiple instances of the variable with the same name, it’s not sure that your code works as expected.
Btw. It’s an absolutly bad idea to define a variable in a h-file, the standard way is to define them in the c-file and only to declare them in the h-file (if you really need to access them).
You use
staticwhen you only want to use the variable in one module, and it should be invisible to all others.const ...only if you really need to access it from another module, but IMHO you should normally avoid global accessible variables.myFile.c
myFile.h