Sign Up

Unlock the Gateway: Join the Embed Threads

Have an account? Sign In

Captcha Click on image to update the captcha.

Have an account? Sign In Now

Sign In

Unlock the Possibilities: Dive In with Login Access

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

Embed Threads

Embed Threads

Embed Threads Navigation

  • Blogs
  • Forum
  • About Us
  • Contact Us
  • Books
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Forum
  • Communities
  • Questions
    • New Questions
    • Must Read Questions
    • Trending Questions
    • Hot Questions
  • Polls
  • Groups
  • Add group
  • Badges
  • Users
  • Help
  • Blogs
  • Forum
  • About Us
  • Contact Us
  • Books
Ganesh Biradar
  • 0
Ganesh BiradarBeginner
Asked: July 6, 20232023-07-06T16:28:35+00:00 2023-07-06T16:28:35+00:00In: Embedded Software Development

Use of static variable declaration in a header file included in multiple .c files

  • 0

I have a header file (.h) that is included in multiple .c files. In this header file, there is a variable declared as static. Since the header file is included in multiple files, there can be multiple accesses to that variable.

I’m curious to know the purpose or usefulness of using the static qualifier in this scenario. What is the significance of declaring a variable as static in a header file that is included in multiple .c files?

cprogrammingscope of variablesstatic variable
  • 4 4 Answers
  • 490 Views
  • 0 Followers
  • 0
Share
  • Facebook

    Sorry this question is closed.

    4 Answers

    • Voted
    • Oldest
    • Recent
    1. Arun
      2023-07-06T22:14:20+00:00Added an answer on July 6, 2023 at 10:14 pm

      In header it’s just declaration, not definition.

      Static variable or function scope is limited to that file only and it lives till the execution of program that means it’s not located on stack which is exists in RAM.

      In short static means scope limited to that file.

      you can use and modify static variables using get and set methods.
      have a read here :

      https://aticleworld.com/local-static-and-global-variables-in-c/

      • 0
      • Reply
      • Share
        Share
        • Share onFacebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    2. Shashank Beginner
      2023-07-10T05:23:38+00:00Added an answer on July 10, 2023 at 5:23 am

      Here we declare variable as a global scope.
      If a variable is declared as static at the global scope within a header file (.h file) that is included in multiple .c files, each translation unit (.c file) will have its own private copy of that variable. This means that each file that includes the header will have its own independent instance of the variable, even though they have the same name.

      However, it’s worth noting that including a header file with static variables in multiple translation units can lead to a waste of memory, as each file will have its own separate copy. In general, it is considered best practice to avoid defining variables in header files and instead declare them as extern in the header and define them in a single source file to maintain a single instance of the variable

      • 1
      • Reply
      • Share
        Share
        • Share onFacebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    3. Ritesh236 Explainer
      2023-07-12T04:11:42+00:00Added an answer on July 12, 2023 at 4:11 am

      Hello,

      Yes. If you declare static variables into header file and that file is included into multiple c or cpp source files then each file contains separate copy of that variables which can be shared and accessed it.

      But, It depends on like how you are going to access it and use it.

      • 1
      • Reply
      • Share
        Share
        • Share onFacebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    4. Best Answer
      fkfkShaktimaan
      2023-07-18T07:05:39+00:00Added an answer on July 18, 2023 at 7:05 am

      When we declare a variable as static, what we are doing is providing the access of that variable to the whole file.
      Any function can in that specific file can access it.

      Generally when we want to share the variable’s value between many files in the project, we set the variable as static and write get/set functions which are external mostly to read the value of static variable.

      • 1
      • Reply
      • Share
        Share
        • Share onFacebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    Sidebar

    Ask A Question

    Stats

    • Questions 85
    • Answers 949
    • Best Answers 15
    • Users 800

    Free PDF Downloads

    A Roadmap to a Successful Career

    HTTP, WS and TLS: The Complete Guide

    Embedded Interview Questions

    • Popular
    • Answers
    • alokm014

      Embedded System Testing: Strategies and Approaches

      • 271 Answers
    • BHASKAR

      RTC IN STM32 DATE PROBLEM

      • 62 Answers
    • Tanmoy Chandra Dey

      Why is the "ADDR flag" cleared without reading the SR2

      • 60 Answers
    • Aditya007

      Programming Error while using PICKIT4

      • 58 Answers
    • D Sai krishna

      STM32 board is not shown in Device Manager ( Not ...

      • 53 Answers
    • sex việt f68
      sex việt f68 added an answer Nice weblog here! Additionally your website quite a bit up… June 7, 2025 at 3:22 pm
    • How Do You Play Two Handed Pinochle
      How Do You Play Two Handed Pinochle added an answer However, if your opponent bids low, you may want to… June 7, 2025 at 2:48 pm
    • https://blackcoin.co
      https://blackcoin.co added an answer If you do not wish to concede, table your natural… June 7, 2025 at 2:46 pm
    • drdavidcreel.com
      drdavidcreel.com added an answer Dianabol is a steroid that is often used for bodybuilding… June 7, 2025 at 1:39 pm
    • Maltepe Masaj Salonu
      Maltepe Masaj Salonu added an answer Hmm is anyone else having problems with the images on… June 7, 2025 at 3:41 am

    Explore

    • Forum
    • Communities
    • Questions
      • New Questions
      • Must Read Questions
      • Trending Questions
      • Hot Questions
    • Polls
    • Groups
    • Add group
    • Badges
    • Users
    • Help

    Footer

    Embed Threads

    Embed Threads

    Join us on a transformative journey where knowledge is amplified and solutions are empowered.

    About Us

    • Blogs
    • Forum
    • About Us
    • Contact Us
    • Books

    Legal Stuffs

    • Privacy Policy
    • Adsense Disclaimer

    Help

    • Help
    • Contact Us

    Follow

    © 2024 Embed Threads. All Rights Reserved
    With Love by Embed Threads.