Plain Constant variables in C++

c++, constants, linkage

Solution

For `C++` it won't be the same due to the internal linkage - these are 2 distinct objects. In `C` it's the other way around and `const` will have external linkage, thus you will get a linkage error due to redefinition.

Problem

Plain constant variables in C++ default to internal linkage. Suppose If I have the following: I define a const variable in a header file(`const int var = 2`) Then I include the header in two cpp files. If I try getting the address of that const variable (i.e `&var`) in both of the cpp files, then will those two addresses be same? Also I need a small working code to verify this fact. I had to post this as a question because I couldn't ask it there in the comments for this answer given in this thread as I'm a newbie.

Original source

Related problems