Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can static declared global variable can be accessed with extern in another file?

Tags:

c

static

global

I have one doubt if i declared global variable with static.

file1.c

static int a=5;

main()
{
   func();
}

can it be access in another file2.c using extern ?

file2.c

func()
{
   extern int a;
   printf(a);
}

or only global variable declared without static can be access using extern?

like image 459
david Avatar asked Nov 30 '22 22:11

david


1 Answers

No!
static limits the scope of the variable to same translation unit.
static gives the variable an Internal Linkage and this variable cannot be accessed beyond the translation unit in which was created.

If you need to access a variable accross different files just drop the static keyword.

like image 94
Alok Save Avatar answered Dec 05 '22 00:12

Alok Save