Interview Questions

How can I make a sort of `semi-global variable

C Interview Questions and Answers


(Continued from previous question...)

How can I make a sort of `semi-global variable

How can I make a sort of ``semi-global'' variable, that is, one that's private to a few functions spread across a few source files?
You can't do this in C. If it's impossible or inconvenient to put all the functions in the same source file, there are two usual solutions:
1. Pick a unique prefix for the names of all functions and global variables in a library or package of related routines, and warn users of the package not to define or use any symbols with names matching that prefix other than those documented as being for public consumption. (In other words, an undocumented but otherwise global symbol with a name matching that prefix is, by convention, ``private.'')
2. Use a name beginning with an underscore, since such names shouldn't be used by ordinary code.
It may also be possible to use special linker invocations to adjust the visibility of names, but any such techniques are outside of the scope of the C language.

(Continued on next question...)

Other Interview Questions