How to share a variable and its value between many c source files?

I have 2 source files (.c) with the names file1.c and file2.c that need to split the variable between them, so if the variable is updated in one source file, then in the other source file, when you access this variable, the change will be visible .

what I did creates another source file called file3.c and a header file named file3.h (which, of course, is included in file1.c file2.c and in file3.c)

in file3.c: int myvariable = 0; void update(){//updating the variable myvariable++; } int get(){//getting the variable return myvariable; } in file3.h: extern int myvariable; void update(void); int get(void); in file1.c: . . . printf("myvariable = %d",get());//print 0 update(); printf("myvariable = %d",get());//print 1 . . . in file2.c: . . . printf("myvariable = %d",get());//print 0 but should print 1 . . . 

but the problem is that update is called in file1.c and myvariable is updated, this change cannot be seen in file2.c , because in file2.c, when get and myvariable are called, it prints, then 0 is printed only if update2.c is called in the file, then this change has been noticed. It looks like the variable is shared, but for each source file for that variable there is a different variable value / different memory.

+6
source share
4 answers

You can declare the variable as extern in the others file when you need the variable ...

+3
source

include file3.h in file1.c and file2.c

+2
source

I recommend avoiding extern variables due to code clutter - repeating externs in every file using this global one. It is usually better to bind a global variable to some area of ​​the file, making it static . And then use the interface functions to access it. In your examples, this would be:

 // in file3.h void update(int x); int get(void); // in file3.c: static int myVariable = 0; void update(int x){ myVariable = x; } int get(){ return myVariable; } // in other files - include file3.h and use // update() / get() to access static variable 
+2
source

Here is just one possible solution. Moreover, the variable is not global for the entire application and can only be read / written using access functions. Please let me know if you have any questions.

Files: access.c access.h file2.c main.c
Compile with: gcc main.c file2.c access.c -o test
Run: ./test

File: main.c

 #include <stdio.h> #include "access.h" int main( int argc, char *argv[] ) { int value; put( 1 ); printf("%d\n", get()); put( getValue() + 1 ); printf("%d\n", getValue()); return(0); } 

File: access.c

 #include "access.h" static int variable = 0; int get( void ) { return(variable); } void put( int i ) { variable = i; return; } 

File: file2.c

 #include <stdio.h> #include "access.h" int getValue( void ) { int i = get(); printf("getValue:: %d\n", i); put(++i); printf("after getValue:: %d\n", get()); return( i ); } 

File: access.h

 extern int getValue( void ); extern int get( void ); extern void put( int i ); 

And here is the result of the execution:

 [ root@jrn SO]# ./test 1 getValue:: 1 after getValue:: 2 getValue:: 3 after getValue:: 4 4 

Hope this helps.

+2
source

Source: https://habr.com/ru/post/914363/


All Articles