Scopes 101
Scopes are one of the biggest factors in programming under the hood and can make or break a program. In fact, bad scope control means producing a program that can take hours to debug. For these reasons, they are important to cover.
First, what is a scope? Formally, a scope within a program demarcates the actionable context of an identifier, or in simpler terms, keeps track of where and how long something can be manipulated and referenced in a program.
For a simple example, consider the below:
Here, x
is now an identifier of type int
(simply, a variable) in the program. But in order to see where and when exactly it makes sense to use x
to refer to this variable, we must consider scope. In this example, x
has been declared in the global scope (and is global), the place in the program before the beginning of any functions.
When a variable is declared globally as such, it is implicitly initialized, meaning that it can be safely evaluated to whatever the corresponding default initialization of the type is. This is a very important property to remember!
To better see this, we can try printing x
:
Global variables can be used anywhere, including functions other than main
and still make sense in the context.
Local scopes, in contrast, are very strict local demarcations separated clearly by pairs of curly braces (and a lot stricter in memory, with greater chance of segmentation faults).
As above, variables declared locally are default uninitialized and must be explicitly initialized, lest one attain "random" values (technically whatever is in the memory at that location). Variables left uninitialized in local scopes result in undefined behavior according to the standard and are best avoided.
To avoid this, we could use either the global scope like before or explicitly initialize x
:
Of course, functions are not the only examples of local scopes; we can also create our own scopes within curly braces as long as these are within functions:
Last updated