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 6701867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:59:07+00:00 2026-05-26T06:59:07+00:00

Take for example: // myheader.h static int myStaticVar = 0; // If we remove

  • 0

Take for example:

// myheader.h
static int myStaticVar = 0;
// If we remove 'static' the compiler will throw linker error.

void DoStuff();

// and myheader.cpp, and main.cpp;  etc

This is how I would explain it:

Static variables do not have external linkage, and when we compile
without ‘static’ we are “including” the static variable (which is
global here) in every file, which create duplicates and linker will
throw an error since multiple declaration is not allowed.

Is there any better way to explain this? Thanks.

PS: Are we suppose to have static variables (not talking about members) in header file?

  • 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-26T06:59:08+00:00Added an answer on May 26, 2026 at 6:59 am

    Why can’t a non-static variable reside in a header file?

    Because it breaks the One Definition Rule(ODR).
    When you include the header file containing a non-static variable, the declaration of the variable gets pasted in each source file where it is included. Thus, you end up having more than one definition of a variable, in the same Translation Unit, this violates the ODR, and hence, the linker will give you linking errors.

    How to explain static variables declared in header files?

    When you declare a static variable in a header file, a copy of the variable gets created in each Translation Unit where the header file is included.

    Declaring a static variable in the header file will not give you multiple definition errors, but it does not achieve your purpose of having a global variable whose value is shared across all files which access it.

    You may think that since you are using a global static variable, its value will be retained across different files, but as mentioned above, each Translation Unit has its own copy of the variable, and it does not achieve what you think you are achieving.

    Are we suppose to have static variables (not talking about members) in header file?

    No, Never!

    How do you declare and define global variables?

    You need to use the extern keyword.

    Add the extern declaration of the variable in a header file. The header should be included by the one source file that defines the variable, and by all the source files that reference the variable. Only one source file should define the variable. Also, only one header file should declare the variable.

    filename.h

    extern int gVariable;  /* Declaration */ 
    

    file1.cpp

    #include "filename.h"  
    
    /* Definition */ 
    int gVariable = 37;    
    
    void doSomething(void) 
    { 
        return gVariable++; 
    } 
    

    file2.cpp

    #include "filename.h" 
    #include <stdio.h>  
    void doSomethingWithGlobal(void) 
    {     
        printf("Global variable: %d\n", gVariable++); 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

take example: -(void)setName:(NSString *)name age:(int)age; How would you call this method (in other words,
Take this example code private void test() { Label1.Text = Function 1 started.; function1();
take the example: $ du -h file_size.txt 112K file_size.txt How would i remove the
Take this example: int[] queryValues1 = new int[10] {0,1,2,3,4,5,6,7,8,9}; int[] queryValues2 = new int[100];
I will take the example of stack overflow itself: Stack overflow displays icons using
Take for example the following code: try { Response.Redirect(someurl); } finally { // Will
Take this example: public class foo { public int[] func() { int arr[] =
Take an example of a question/answer site with a 'browse' slideshow that will show
Take the example here: public static IEnumerable<BigInt> EvenNumbers(IEnumerable<BigInt> numbers) { foreach (BigInt number in
Let's take example of a command example-command. I open terminal I write example-command in

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.