user928774
user928774

Reputation:

how to share a variable and its value between many source file in c language?

i have 2 source files (.c) named file1.c and file2.c which need to share between them a variable, so that if in one source file the variable is been updated then in the other sourced file when accessing this variable the change will be seen.

what i did is create another source file called file3.c and header file called file3.h (which, of course, is been 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 when in file1.c update is invoked and myvariable is updated the change cannot be seen in file2.c because in file2.c when get is invoked and myvariable is printed then 0 is been printed, only if in file2.c update is invoked then the change is been seen. it seems like the variable is shared but for each source file there is a different variable value/different memory for this variable

Upvotes: 6

Views: 1890

Answers (4)

Chimera
Chimera

Reputation: 6018

Here is just one possible solution. Doing this the the variable isn't global to the entire application and can only be read/written to using the access functions. Please let me know if you have 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 run output:

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

I hope this helps.

Upvotes: 2

Agnius Vasiliauskas
Agnius Vasiliauskas

Reputation: 11267

I recommend to avoid extern variables because of code clutter - repeating externs in each and every file using that global. It is usually better to bind global variable to some file scope by making it static. And then to use interface functions to access it. In your example terms it will 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

Upvotes: 2

keety
keety

Reputation: 17441

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

Upvotes: 2

aleroot
aleroot

Reputation: 72636

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

Upvotes: 3

Related Questions