Access a global static variable from another file in C
Asked Answered
F

5

11

In C language, I want to access a global static variable outside the scope of the file. Let me know the best possible way to do it. One of the methods is to assign an extern global variable the value of static variable,

In file a.c

static int val = 10;
globalvar = val;

In file b.c

extern globalvar;

But in this case any changes in val(file a.c) will not be updated in globalvar in (file b.c).

Please let me know how can I achieve the same.

Thanks, Sikandar.

Fao answered 29/12, 2009 at 6:9 Comment(1)
For those wondering why someone would want to do this: I am encountering a similar situation with needing to access a command table inside generated code, developing in the manufacturer's IDE for their programmable hardware.Cabal
W
25

Well, if you can modify file a.c then just make val non-static.

If you can modify a.c but can't make val non-static (why?), then you can just declare a global pointer to it in a.c

int *pval = &val;

and in b.c do

extern int *pval;

which will let you access the current value of val through *pval. Or you can introduce a non-static function that will return the current value of val.

But again, if you need to access it from other translation units, just make it non-static.

Whitecap answered 29/12, 2009 at 6:13 Comment(0)
M
10

You can make the global variable pointer to the global static variable.

/* file  a.c */
static int a = 100; /* global static variable not visible outside this file.*/
int *b = &a; /* global int pointer, pointing to global static*/


/* file b.c */
extern int *b; /* only declaration, b is defined in other file.*/

int main()
{
        printf("%d\n",*b); /* dereferencing b will give the value of variable a in file a.c */
        return 0;
}

On running:

$ gcc *.c && ./a.out
100
Mushy answered 29/12, 2009 at 6:18 Comment(0)
P
6

You cannot access a file level static variable outside of the file.

If you truly need to do that, you have a couple of choices.

  1. Add an accessor function to the file that has the static variable. The nice thing is this restricts access from outside the file to read-only access:

    int read_static() { return val; }

  2. Drop the static qualifier and make the variable a global.

Pachton answered 29/12, 2009 at 6:12 Comment(2)
Almost: you cannot access it outside of that TU (translation unit). An understanding of TUs and realizing file-level static means "TU-local only" is what he needs.Eiser
@Roger - you are absolutely correct. But I remember when I was a newbie C programmer that "translation unit" always confused me.Pachton
E
2

Solution 1:

In file a.c

static int val=10;
int *globalvar =&val;

In file b.c

extern int *globalvar;

Solution 2:

Instead of having another variable to pass the address of the static variable thereby adding few memory bytes wastage, make the static variable itself as extern.

Solution 2 is recommended, but in case if you are restricted to changing the static variable to extern, use solution 1.

Elaterite answered 29/12, 2009 at 7:38 Comment(0)
S
0

To access the static variable from another file in C, we can achieve this by using a function or a pointer, the choice depends on the specific requirements and design considerations of your program.

Use a Function: Create a function in the file where the static variable is defined, and that function returns the value of the static variable. This way, the variable itself remains static, but you provide controlled access through a function.

// File1.c
static int myStaticVariable = 10;

int getStaticVariable() {
return myStaticVariable;
}

// File2.c
#include <stdio.h>

extern int getStaticVariable();  // Function prototype

int main() {
int value = getStaticVariable();
printf("Static Variable: %d\n", value);
return 0;
}
Sauterne answered 11/1, 2024 at 20:27 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.