variables declared in two files..

Posted on 2006-04-06
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!
Question by:payalpr2002
    LVL 48

    Accepted Solution

    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.

    Author Comment

    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?
    LVL 48

    Expert Comment

    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 Comment

    Thanks a lot!

    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Article by: SunnyDark
    This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
    Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now