The ‘C’ test. Part – VI

Static


6. What are the uses of the keyword static?

This simple question is rarely answered completely. Static has three distinct uses in C:

  • A variable declared static within the body of a function maintains its value between function invocations
  • A variable declared static within a module, (but outside the body of a function) is accessible by all functions within that module. It is not accessible by functions within any other module. That is, it is a localized global
  • Functions declared static within a module may only be called by other functions within that module. That is, the scope of the function is localized to the module within which it is declared

Most candidates get the first part correct. A reasonable number get the second part correct, while a pitiful number understand the third answer. This is a serious weakness in a candidate, since he obviously doesn’t understand the importance and benefits of localizing the scope of both data and code.