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
code

#include<stdio.h>
#include<conio.h>

main()
{
extern int *ptr;
printf("%d\n", *ptr);
func();
printf("%d", *ptr);
getch();
}

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
file1.c

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

Write a program that takes a 5 digit number and calculates 2 power that number and prints it

5 Answers   ABS, Accenture, HCL, Infosys, Infotech, SoftSolve, Software India, TCS, Vertex, Vimukti Technologies,


Subtract Two Number Without Using Subtraction Operator

0 Answers  


how can we print  hellow world programme without using semicolon

3 Answers  


What does the function toupper() do?

0 Answers  


Write a Program to print this triangle: * ** * **** * ****** * ******** * ********** use two nested loops.

12 Answers   MIT, TCS,






What is difference between function overloading and operator overloading?

0 Answers  


How is a macro different from a function?

0 Answers   Tech Mahindra,


Which is the memory area not included in C program? give the reason

0 Answers   IBM, TCS,


Hai,I have done with my bachelor of commerce and planing to ms,please suggest me how to convince vo for shifting from commerce to computers. Visa on 8 DEC 2014  Npu university

0 Answers  


What are dangling pointers in c?

0 Answers  


Why does the call char scanf work?

0 Answers  


what are the advantage of pointer variables? write a program to count the number of vowels and consonants in a given string

3 Answers  


Categories