Next: Exercises Up: Further Data Types Previous: Enumerated Types

Static Variables

A static variable is local to particular function. However, it is only initialised once (on the first call to function).

Also the value of the variable on leaving the function remains intact. On the next call to the function the the static variable has the same value as on leaving.

To define a static variable simply prefix the variable declaration with the static keyword. For example:

Output is:

Clearly the auto_var variable is created each time. The static_var is created once and remembers its value.
Wed Sep 14 10:06:31 BST 1994