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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:31:02+00:00 2026-06-06T13:31:02+00:00

I wrote in // In file t.h #ifndef __t_h__ #define __t_h__ static int abc;

  • 0

I wrote in

// In file t.h

#ifndef __t_h__
#define __t_h__
static int abc;
#endif

—

//In   main.c
    #include <stdio.h>
    #include "t.h"
    int main()
    {
        abc++;printf("%d \n", abc);
        test();
    }

– –

//In test.c

#include <stdio.h>
#include "t.h"

void test()
{
    abc++;
    printf("%d \n", abc);
}

When I run the project I found the output of abc is 1 and 1.
But when I change it to int abc in t.h. The output of abc = 1 and 2.
Why static is not retaining the value when the control reaches to test.c file.
If it won’t retain then why should not it provide an error as the static variable can not be shared between/among the files?

  • 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-06T13:31:03+00:00Added an answer on June 6, 2026 at 1:31 pm

    static variables has internal linkage which means each translation unit gets its own copy.

    So in your program each .cpp file which includes t.h has its own copy of the static variable, which in turn means, there are two objects in the memory. You can try printing their addresses to confirm this, as they will be different.

    That makes the situation pretty simple: if you change the object in one .cpp, it doesn’t reflect in the other .cpp file, because the object in the other .cpp file is a different object. Why should it change?

    But when you change it to int abc (i.e don’t make it static), then each translation unit has same object. If you change it in one file, it will be reflected in other file also, as expected.


    As for sharing, then yes, static objects can be shared between two functions in the same translation unit, but they cannot be shared between two translation units.

    Search for translation unit on this site, you will get many topics on it. Read them, then you will understand it fully.

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

Sidebar

Related Questions

Following program: #include <stdlib.h> #include <stdio.h> #include <sys/stat.h> #include <sys/types.h> int main() { fclose(
i wrote in file showList.php the following form, which select items from the database
I'm new to org-mode and wrote a file with Babel in a few languages.
I made an website with php and I wrote .htacess file in my htdocs
Possible Duplicate: Global variables in R ad the beginning of the file I wrote:
I wrote some code to read a file in my Java Servlet class. (I'm
I wrote XML type in my XSD file: <xs:simpleType name=refId> <xs:restriction base=xs:ID> <xs:maxLength value=30/>
I wrote the following code to count the node of an XML file: private
I wrote a function that scans a tab delimited file of baseball stats. public
I wrote this script which counts occurrences of particular pattern in a given file.

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.