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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:27:33+00:00 2026-06-09T18:27:33+00:00

static keyword keeps the scope of a global variable limited to that translation unit.

  • 0

static keyword keeps the scope of a global variable limited to that translation unit.
If I use static int x in a .h file and include that .h file every other file, won’t they all belong to the same translation unit?
Then, won’t x be visible everywhere?
So what is the role of static now?

Also, is there any use of static const int x ,where x is a global variable?
Aren’t all const global variables static by default?
And is a const variable’s scope limited to the TU even if it confined in a for loop in the 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-06-09T18:27:34+00:00Added an answer on June 9, 2026 at 6:27 pm

    If you write

    static const int x
    

    in an .h file then every translation unit that #include-s this .h will have its own private variable x.

    If you want to have 1 global variable visible to everyone you should write

    extern const int x;
    

    in the .h file and

    const int x = ...;
    

    in one of the .cpp files.

    If you want to have a static const int visible to just one translation unit – don’t mention it in the .h files at all.

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

Sidebar

Related Questions

In C++ it is possible to use the static keyword within a translation unit
I wonder about the use of the static keyword as scope limiting for variables
Can someone explain when you're supposed to use the static keyword before global variables
I have read somewhere that we can restrict the scope of global variable to
Is the static keyword in C used only for limiting the scope of a
My understanding is that the static keyword was introduced for compatibility with .NET (along
When is it considered poor practice to use the static keyword in Java on
With the new static keyword in PHP 5, is there any reason to use
I thought up a good use of the static keyword inside a function to
How does the static keyword works internally? Considering the base definition static variable is

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.