-6

HI I welcome all to answer this. Q1) if a File1.c contains static int a; and File2.c contains static int a;

if these two files are part of same project.

will the compilation throw any error if so why? if it wont throw any error why? as we declared with same..?

Q2) a static variable X; declared in prog1.c how can we use the value of X, in prog2.c

tell me the other way without sending the value throuh return value from a function declared globally in prog1.c

note: some body told me about GET and SET functions how can we use this to get the static var value..? is there any other way to access the value.?

Chaitanya
  • 1
  • 1
  • 2

3 Answers3

2

The whole point of the "static" keyword is that it means the variable (or function) is only available within the file in which it is defined. So these two variables are entirely separate. Therefore no error and also no possibility of accessing one from the other file.

Mark
  • 21
  • 2
0

will the compilation throw any error if so why? if it wont throw any error why? as we declared with same..?

No not. It's have scope for particular file in which it declared due to static storage class. Using static for global variables and functions means they're only visible to that translation unit.

Q2) a static variable X; declared in prog1.c how can we use the value of X, in prog2.c

You can't extern it again due to static.It's private to the source file that it is declared in.

EDIT:

You say how i access static variable from one file to other. But to do so what is meaning of static keyword? Doing this best approach is just remove static keyword.

by bad codding style you can access by using function like.

static int myvar;


int * accesssaticvar() {
   return &myvar;
}
Jayesh Bhoi
  • 24,694
  • 15
  • 58
  • 73
  • Hi as u mentioned that we can't use extern as it is a static var, that I have in my mind earlier. the question is how to access a static variable declared in one file in other. is there any way to access – Chaitanya Jun 16 '14 at 06:21
  • @user3732905 If you want to access in another file then what is meaning of `static` keyword? just drop it. – Jayesh Bhoi Jun 16 '14 at 06:23
  • Hmm actually this is a interview question I told him the same that remove the static before variable he said that that I want to access that static variable only in my file tell the way to get the value? I told him to declare a function globally and return the static var – Chaitanya Jun 16 '14 at 08:36
0

Hmm actually this is a interview question I told him the same that remove the static before variable he said that that I want to access that static variable only in my file tell the way to get the value?

I told him to declare a function globally and return the static var, as the the function only return the value by calling that function we can get the value? he said some GET and SET function usage instead of returning from function. something which i dont konw ...

I even realized that what is the meaning of defining a var as static if we want to break the rule of static.. either the question might be stupid ... or there is a way to access private variable....

Chaitanya
  • 1
  • 1
  • 2