We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

variables declared in two files..

payalpr2002
payalpr2002 asked
on
Medium Priority
279 Views
Last Modified: 2010-04-01
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!
Comment
Watch Question

Commented:
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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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?

Commented:
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

Author

Commented:
Thanks a lot!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.