Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5977009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:18:33+00:00 2026-05-22T21:18:33+00:00

Static variable has the scope inside that file only where they are been declared,

  • 0

Static variable has the scope inside that file only where they are been declared, as shown in below code:

file1-

static int a;

file2-

extern int a;

This will give linking error as static variable a has the scope in file1 only. But I am confused with below code:

file2-

#include "file1"
extern int a;

Here it would not give any linking error. Then it means compiler is refering “a” which is declared in file1. But when you debug you will find address of variable “a” is different in file1 and file2. Is the compiler creating a another global variable “a” in file2?

complete code-

file temp1.h –

static int w = 9;

class temp1 
{
public:
   temp1(void);
public:
   ~temp1(void);

   void func();

};

……………………cpp……………..

temp1::temp1(void)
{
   int e =w;
}

temp1::~temp1(void)
{
}
void temp1::func()
{
}

…………………………………
file2-

#include "temp1.h"

extern int w;
int _tmain(int argc, _TCHAR* argv[])
{
   w  = 12;

   temp1 obj;
   return 0;
}

here when i debug and check the value and adrress in temp1 constructor and in file2 is different.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:18:33+00:00Added an answer on May 22, 2026 at 9:18 pm

    file1:

    static int a;
    

    file2:

    extern int a;
    

    There are two variables referenced here. The first is a declaration and definition of a variable with internal linkage in file1; the second is a declaration only of a variable with external linkage in file2. This doesn’t necessarily cause an error; it is completely legal to have a variable with external linkage used in some translation units and identically named variables with internal linkage used by other translation units. Any link error would only occur if a is used in file2 and there isn’t a definition for this a in file2 or any other translation unit in the program.

    #include "file1"
    extern int a;
    

    In this example you have combined the two files into a single translation unit. The variable a is first declared and defined in file with internal linkage due to static, the second is just a re-declaration which doesn’t alter the previous linkage. The second declaration is redundant in this instance. If you are still compiling both files separately as well as including file1 from file2 then each translation unit (file1 and file1 + file2) will have its own distinct variable called a.

    Note that if you had used extern int a; followed by static int a; then this would be a compile error as the first declaration would declare a to have external linkage if no previously declaration was visible and then the second declaration and definition would cause an error because the linkage implied by static int a; would conflict with the previous declaration.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Static variable has file scope. Say I have two following files: file1.h file1.cpp file2.h
I am working with code that has a global static variable (which is an
Suppose I have a static variable declared inside a function in C. If I
I have a class MyClass that has a static variable. Class implements smth like
I have a class with static method which has a local static variable. I
If I have a global static variable x like in this code #include <stdio.h>
In the below program: class Main { static string staticVariable = Static Variable; string
Is there a way to have a mutable static variable in F# class that
Suppose I have two classes that both contain a static variable, XmlTag. The second
I have a Unit testing problem where a class has a static variable which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.