Variables in C

Note

Variables in programming languages need to be destinguised with respect to “visibility” and “lifetime”. The term “scope” combines both and leaves it sometimes unclear what was meant.

visibility

The places where a variable that has been defined somewhere can be used. A variable may not be visible for certain sections of code and therefore not be usable there. But this does NOT imply that the variable is no longer used at all and could be collected.

lifetime

Only when there is NO code for which the variable is visible can it be safely collected. This is e.g. the job of a garbage collector.Automatic variables (stack variables) are visible ONLY within the function that defines them. C as well as Java are so called "block-oriented" languages. A variable defined in an outer block is visible inside but not vice versa.