Jump to content

C++ Language/Variables/LifetimeAndScope

From Wikibooks, open books for an open world

A variable defined outside of all functions will have "static-lifetime" (it will exist throughout your software's entire duration). A variable defined inside some function could also have static-lifetime, but it would need to be marked by the static keyword. Otherwise, it would be a transient "local-variable" whose storage will disappear when that function ends.

A variable defined inside some function has "local-scope" (it can only be used from within that function, regardless of its lifetime). A variable defined outside of all functions has "global-scope" (it can be used by all subsequent functions). If you mark a globally-scoped variable by the static keyword, then it can only be used in this source file.

Additional information about lifetime and scope