Answered Questions

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

    Star Read Best Answer

    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 if you try to use it in another file the compiler finds the variable prefix and flags out an error.

    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.


    • 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.