Can we use static variables in file2 if they are defined in file1 ? If yes, then how ?

Editorial / Best Answer

Answered by: Kranthi Kiran

  • Dec 2nd, 2006


We cannot use a 'static variable' declared in one file and use it in file 2.Reason: The purpose of 'static' keyword is to retain the value betwwen the function and to restrict thescope of the variable to the file.When a variable is declared as static it will be given memory in global area portion of the process segments with the variable mname prefixed with the file namefor Example: static int k; implies it is stored as "filename.k" in global segment.so if you try to use it in another file the compiler finds the variable prefix and flags out an error.

Showing Answers 1 - 64 of 64 Answers

Venkat

  • Nov 10th, 2006
 

Since the variable needs to be used in file 2 I think we can use the extern keyword to pull the variable into File 2. Please pass on your comments.

bright

  • Nov 10th, 2006
 

Hmm. Can we just declare the variable in file2 as static.

I did that in my programs.

  Was this answer useful?  Yes

Chitta Ranjan Ray.TCS-KOCHI

  • Nov 21st, 2006
 

Yes we can acess a variable declaired in a file from another file.For that we have to specify that variable as extern.But make sure that extern variable is for only declairation purpose not for defination.That is we cant initialise any value to the variable while declairing as extern. 

Kranthi Kiran

  • Dec 2nd, 2006
 

We cannot use a 'static variable' declared in one file and use it in file 2.Reason: The purpose of 'static' keyword is to retain the value betwwen the function and to restrict thescope of the variable to the file.When a variable is declared as static it will be given memory in global area portion of the process segments with the variable mname prefixed with the file namefor Example: static int k; implies it is stored as "filename.k" in global segment.so if you try to use it in another file the compiler finds the variable prefix and flags out an error.

kalayama

  • Dec 5th, 2006
 

Wonderfully explained Kranthi.Well Chitta Ranjan what you say is right. But if you are saying one should declare a variable like extern staic int then you are wrong. We can't have two storage specifiers for a single Variable declaration.If one needs to use the variable in multiple files, then he needs to declare it has extern. The purpose of static is to make the variable "invisible" for external applications.Hence, there is no meaning for trying toaccess static variable from another file. Use extern instead.

  Was this answer useful?  Yes

suresh

  • Dec 27th, 2006
 

yes,my answer for this question is ,first of all static variables are used whenever you want a variable to be in file scope using it in some other file is useless and still if you want it then you don't be acheving your desired functionality.

Here is a little bit of xplanation reg this question:

First if you declare any variable in .C file and you use it as extern in the other .C file you won't get compile errors bcoz the compiler is happy at compilling time by seeing the extern key word but when it tries to link then it complains abt undefined reference to tht variable,the reason is since static variable is defined in the file scope and is unavailable for the others.

I think by using extern this is the problem one will get if the varaible is static,if it is not static then its ok.

  Was this answer useful?  Yes

Sameer

  • Jan 4th, 2007
 

No you can't....Mr Kranthi is absolutely rite...

  Was this answer useful?  Yes

Mahesh Shaha

  • Jan 12th, 2007
 

You can not use same static veriable in two files.

  Was this answer useful?  Yes

manoj kumar

  • Feb 4th, 2007
 

No, we can't because static variable has a scope limited to the file in which it is defined. So, we can't use the static variable in file2

  Was this answer useful?  Yes

dasam

  • Mar 29th, 2007
 

If i have a static variable static iCount in one file. lets say test1.h. iam including test1.h in test2.h, can i access the variable iCount from test2.h.

  Was this answer useful?  Yes

Indrajit

  • Jul 23rd, 2007
 

First of all it's bad practise to declare variables in header files.

Next, header files are preprocessed and become part of the file which is the output after preprocessing the CPP/C files in which they are included..

so the effect of having a static variable in a header file and including that in any file would be as if you have declared the static variable in the file in which you included the header, and it's scope would be limited to that file only... i.e. you cannot "extern" the same varibale from another file and start using it. That would be an error.
Also if you include the same header in another file then the same static variable there would be treated as a different variable altogether whose scope would be limited to that file only.

mkulacz

  • Jun 2nd, 2009
 

Write two files:

t1.c:

static int iii = 4;

t2.c:

etern int iii;
main() { printf("%dn",iii);}

Both files will compile, but you will get a linker error (from ld in the case of gcc on linux) that t2.c has an undefined reference to "iii".

The answer is No. There may be a way to hack around it, but who cares.

yzesong

  • Jul 31st, 2009
 

Any static variables or functions in file2 scope can only have internal linkage, no external linkage. So the answer is NO.

  Was this answer useful?  Yes

prakash G

  • Jul 18th, 2011
 

to using pointer to function method for accessing static function from one file to another file

  Was this answer useful?  Yes

prakash G

  • Jul 18th, 2011
 

USING pointer to function method to accessing static function one file to another file

  Was this answer useful?  Yes

Archana Chandini

  • Jul 29th, 2011
 

Can you use a variable in a file using extern which is defined as both static and global in base file?

can anyone help me in this???

  Was this answer useful?  Yes

sukrandhawan

  • Jul 29th, 2011
 

no static variables cannot be used across multiple files. It restricts the scope of the variable to that file . so not possible

  Was this answer useful?  Yes

neeraj

  • Aug 3rd, 2011
 

No, Static member has only file scope they can not be accessible outside the file in which they defined.

  Was this answer useful?  Yes

srinivas

  • Aug 14th, 2011
 

The storage class Static itself says that, if any variable is declared as static it is private to that particular file, so it cannot be used in another file.if really you want to declare a variable as static and use in diff .c files, then declare that variable as static in that .h file and use the .h file all the .c files..simple-:)

  Was this answer useful?  Yes

karpagam

  • Oct 12th, 2015
 

Code
  1. #include<stdio.h>

  2. int main()

  3. {

  4. int y=5;

  5. void fun(int );

  6. fun(y);

  7. return 0;

  8. }

  9. void fun(int h)

  10. {

  11. static int d=h;

  12. printf("%d",d);

  13. }

  Was this answer useful?  Yes

eshwar

  • Oct 12th, 2015
 

I am getting Error

Code
  1. /***********************    file.c ********************************/

  2. #include<stdio.h>

  3. #include<conio.h>

  4.  

  5. extern void abc(int j);

  6. extern int i;

  7. void main()

  8. {

  9.         abc(i);

  10.         printf(" %d",i);getch();

  11.  

  12. }

  13.  

  14. /***************************  File2.c ****************************************/

  15. #include<stdio.h>

  16. #include<conio.h>

  17. static int i;

  18. void abc(int j)

  19. {

  20.         for(j = 0 ; j <10 ; j++)

  21.         {

  22.                 i++;

  23.         }

  24.         printf( "----------- %d", i);

  25. }

  26.  

  Was this answer useful?  Yes

shreekant

  • Nov 5th, 2015
 

Nice explained by Kalayama "We cant have two storage specifiers for a single Variable declaration. If one needs to use the variable in multiple files, then he needs to declare it has extern." Its simple thing.

  Was this answer useful?  Yes

Vineet Srivastava

  • Nov 13th, 2015
 

Syntatically you cannot as static is meant for one file access only. But still if you want - you can do it by passing it by reference to another file through some function.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions