variables declared in two files..

I have 2 cpp files in a project. I added following 2 lines in both the files:
     int c;
     const int p = 10;

When i build this project, I got a linking error that "int c already defined", which is obvious.

I am wondering, why it did not gave the same error for the 'const' variable?

Can anyone please help me on this?

Thanks in advance!
payalpr2002Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlexFMCommented:
In C, constant values default to external linkage, so they can appear only in source files. In C++, constant values default to internal linkage, which allows them to appear in header files.

This means, const value is defined as static - local to every cpp file where it appears.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
payalpr2002Author Commented:
Thanks. This is clear.
But I am just curious about following things:
1. Is there any specific reason why scope of these const variables is kept local to the file?
2. Does this mean if we apply 'extern' keyword it won't have any effect on const and static variables?
2. Also, can we say that scope of other variables such as 'int a' is global to the application? But we have to use 'extern' keyword to use this variable in other files. That means the scope is not global & it is local to the file. Then why it gave the above error?
0
AlexFMCommented:
1. This is done to allow placing const variables to h-file, which must replace #define.
2, 3. extern is not applied to static and const variables, because they are always local.
extern means: there is global variable defined somewhere else. Compiler knows type of this variable, but doesn't care about variable itself, allowing to linker to do this.
Usually it is done by such way:

// definitions.h

extern int n;

// file1.cpp
#include "definitions.h"

int n;        // variable is defined here

// using n variable

// file2.cpp
#include "definitions.h"

// using n variable - compiler knows that it has int type, variable itself is found later by linker
0
payalpr2002Author Commented:
Thanks a lot!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.