Access of static variable from one file to another file

21,205

Solution 1

1) does the above program violate static variable rules?

No you are not vailoting any rules. Here foo function create copy of value of that static variable and used in other file. Its fine.

2) If not why is this so, and is there any other way to access static variable except including file (#include<>) not like this How am I able to access a static variable from another file?

Static variable are only mean to use in that file only.

You can not use that variable making them extern in other files.

Another dirty hack is to get pointer of that static variable and make that as global pointer and making that as extern in another file you can use that static variable.

file1.c

 #include<stdio.h>
  static int a=25;
  int* ptr = &a;

file2.c

#include<stdio.h>
extern int *ptr;

   int main()
   {
          printf("%d",*ptr);
          return 0;
   }

Correct me if I'm wrong with static variable concept and if any better solutions are available?

  1. A static variable has a lifetime extends across the entire run of the program

  2. If you do not initialize static variable with some value then its default value would be 0.

  3. A static variable has scope limited to its file only. You can not access it by name from a different file.

  4. You have temp1.c and temp2.c both are getting compiled together then also you can have static variable of same name in both files — and they are separate variables.

In C, how do I restrict the scope of a global variable to the file in which it's declared?

By making that global variable as static.

Solution 2

What we commonly call a variable in C is actually two things: an object, the memory allocated for the variable interpreted with a certain type, and an identifier, one way to access that object.

There is no problem in accessing a static object or its value from another compilation unit. Your function foo promotes the value to another unit, that is fine, but it could even promote the address of a without problems.

Having internal linkage only concerns the identifer, the name a. This one is only visible inside file2.c.

Share:
21,205
vinay hunachyal
Author by

vinay hunachyal

Interested in Reading Novels, watching movies, listening songs Embedded linux ,C , Data structure. Helping others #SOreadytohelp

Updated on July 08, 2020

Comments

  • vinay hunachyal
    vinay hunachyal almost 4 years

    I recently came across the question like how to access a variable which declared static in file1.c to another file2.c?

    Is it possible to access static variable?

    My understanding about static keyword in C is,

    static is "internal linkage", so they are accessible only from one compilation unit - the one where they were defined. Objects declared with internal linkage are private to single module.

    As one of my friend suggest me below solution.

    In file1.c

       #include <stdio.h>
    
       int main()
       {
              int b=foo();
              printf("%d",b);
              return 0;
       }
    

    in file2.c

       static int a=25;
    
       int foo()
       {
            return a;
       }
    

    compiled by gcc file1.c file2.c -o file

    If I do above I can access the variable.

    So my questions are:

    1. Does the above program violate static variable rules?

    2. If not, why is this so, and is there any other way to access static variable except including file (#include <…>) not like this.

      How am I able to access a static variable from another file?

      In C, how do I restrict the scope of a global variable to the file in which it's declared?

    3. Correct me if I'm wrong with static variable concept and if any better solutions are available to access static variable?

    • Don't You Worry Child
      Don't You Worry Child almost 10 years
      You have never used a inside file1.c. I think if you try to access it, compiler should throw error.
    • WhozCraig
      WhozCraig almost 10 years
      @Don'tYouWorryChild that would be the linker; not the compiler that would puke, but your point of error generation is just as valid regardless.
    • WhozCraig
      WhozCraig almost 10 years
      You're not violating anything, nor are you "accessing" a outside the translation unit where it is static. You have a function that returns a by-value. If you need to modify it outside file2.c either return its address via a function or provide a functional get/set api.
    • M.M
      M.M almost 10 years
      IDK what "better" is supposed to mean here, but you could write int *foo() { return &a; }
    • vinay hunachyal
      vinay hunachyal almost 10 years
      @Matt McNabb one of the interviewer asked me above question , so i just want to know static variable access is possible or not
    • M.M
      M.M almost 10 years
      @vinayhunachyal yes it is possible. Mr.32 gave one method and my comment gives another. It's a matter of opinion which one is "better"
    • vinay hunachyal
      vinay hunachyal almost 10 years
      @Matt McNabb thank you
    • Don't You Worry Child
      Don't You Worry Child almost 10 years
      @WhozCraig : You are right, it just slipped out of my mind that of course linkage will be defined by linker. Thanks anyways for correcting me... :-)
    • Jonathan Leffler
      Jonathan Leffler almost 10 years
      Note that static hides the name; you can't use that name to access that variable from outside the translation unit (TU) where the static variable is defined. Another variable with the same name may exist in another TU — but that will be a separate variable. But static only hides the name. Pointers can be passed around to make the variable accessible — it is only the name that is completely hidden. But only code within the TU where the static variable is defined can provide access to the variable; ordinary code outside the file can't access the variable by name.
    • Ciro Santilli OurBigBook.com
      Ciro Santilli OurBigBook.com over 5 years
  • vinay hunachyal
    vinay hunachyal almost 10 years
    with static keyword i need to access
  • Jeegar Patel
    Jeegar Patel almost 10 years
    -1 making extern int a you can not access static int a in file1.c.
  • vinay hunachyal
    vinay hunachyal almost 10 years
    is there any better solution to access a static variable in other file?
  • vinay hunachyal
    vinay hunachyal almost 10 years
    is there any better solution to access a static variable in other file? other than what i mentioned above
  • Jeegar Patel
    Jeegar Patel almost 10 years
    i have said static are do not mean to use it in another files. If you want to do such them why you are making that variable as static? just remove static word and access it in another file just adding extern int a.
  • Jeegar Patel
    Jeegar Patel almost 10 years
    still you want to access static variable in another file then Another dirty hack is to get pointer of that static variable and make that as global pointer and making that as extern in another file you can use that static variable.
  • Jeegar Patel
    Jeegar Patel almost 10 years
    @vinayhunachyal I have update code for accessing static from another files.
  • legends2k
    legends2k almost 10 years
    @Mr.32 Please read the answer properly. I said a will have internal linkage and thus the linker cannot see it which means the same. Also I'd written removing static will make it work with the extern. This answer pertains to the first edit of the question the OP used extern int a; in file1.c.
  • Jeegar Patel
    Jeegar Patel almost 10 years
    You should update your answer with clarifying "I'd written removing static will make it work with the extern" so i can upvote to you
  • legends2k
    legends2k almost 10 years
    @Mr.32 Agreed, point taken. Fixed it now to match the changed question.