Follow Our FB Page << >> for Daily Laughter. We Post Funny, Viral, Comedy Videos, Memes, Vines...

can any one tell that i have a variable which is declared
as static but i want this variable to be visible to the
other files? how?

Answers were Sorted based on User's Feedback

can any one tell that i have a variable which is declared as static but i want this variable to be..

Answer / vadivelt

It is not possible to directly access the static variable
of one file from another file. But we can access is it
in different way(by accessing the location).

Lets have a file1.c with following code:

static int a;
int *ptr = &a;/*ptr in Global scope, so by default its
storage class is extern*/

void func(void)
a = 30;

Here 'a' is static variable and it cannot be directly
accessed from another file. So create a pointer of same
data type (*ptr) and assign the address of the variable to
the pointer. Now whenever the variable value is changed in
file1.c, it will be updated in the location of 'a' and
pointer holds that location.

Now Lets have a second file with name main.c with the foll


extern int *ptr;
printf("%d\n", *ptr);
printf("%d", *ptr);

Since *ptr is a extern variable and it holds the address of
a, whenever the value of the variable is changed in
file1.c, it will reflect in main.c also.

Here, when 1st printf() is executed, 'a' value would be 0.
Cos it is a static and defualt initial value is 0.

before 2nd printf() is encountered, 'a' value is changed to
30 in file1.c using func();

since we are accessing address of the variable using ptr
,the same value will reflect in main.c also.

In the same way if u want to change the value of static
variable in main.c

Just use *ptr = somevalue; it will reflect in variable 'a' in

Is This Answer Correct ?    3 Yes 0 No

can any one tell that i have a variable which is declared as static but i want this variable to be..

Answer / hehe

Make the static variable as public

Is This Answer Correct ?    0 Yes 2 No

Post New Answer

More C Interview Questions

Whether there can be main inside another main?If so how does it work?

14 Answers   Sail, Wipro,

main() { unsigned int k = 987 , i = 0; char trans[10]; do { trans[i++] =(char) (k%16 > 9 ? k%16 - 10 + 'a' : '\0' ); } while(k /= 16); printf("%s\n", trans); }

4 Answers   Vector,

write a programming in c to find the sum of all elements in an array through function.

0 Answers  

wat is the output #define VOLEDEMORT _who_must_not_be_named int main() { printf("VOLEDEMORT"); }

2 Answers  

How do you initialize function pointers? Give an example?

3 Answers  

What are preprocessor directives in c?

0 Answers  

Can a variable be both const and volatile?

0 Answers  

in iso what are the common technological language?

0 Answers  

parkside's triangle.. create a program like this.. enter the size: 6 enter the seed: 1 output: 1 23 456 7891 23456 789123 sample2: enter the size: 5 enter the seed: 3 output: 3 45 678 9123 45678 parkside should not exceed 10 while its seed should only be not more than 9..

4 Answers  

triangle number finding program...

1 Answers   HCL,

pierrot's divisor program using c or c++ code

0 Answers  

How can I write a function that takes a format string and a variable number of arguments?

0 Answers