Programming Basics: Scopes

Muhamed Cicak
3 min readJun 11, 2024
No no… Not those kind of scopes but, I love Call Of Duty 4 and we can make a valuable analogy here. The scope you see above basically defines a circle in which it amplifies your vision. In some way, it defines a boundary.

Scopes are an easy concept to wrap your head around but nevertheless a very important one. To be able to continue with other concepts like loops, functions, etc., we have to have a good understanding of scopes. Many tutorials skip this and explain scopes together with other concepts, but I believe scopes are very important to be studied as a separate concept.

Scopes and local variables

Note: identifiers are names that give identity to variables, functions, classes, structures etc. So far, we have only seen variables.

Scopes are boundaries for our pieces of code. They limit all identifiers inside that boundary (scope). The identifiers only exist within the scope they are defined in. The syntax for scopes are the curly braces:

{ 
// some code
}

So, for example, let’s say we have three scopes, called X, Y and Z respecitvely:

Scope X:

{
int a = 5;
// some code
// we cannot access the b variable from the Y scope here
}

Scope Y:

{
int b = 10;
// some code
// we cannot access the a variable from the X scope here
}

Scope Z:

{
// some code
// we cannot access either a or b as they do not exist in the current scope
}

We cannot access the variables defined inside scope X from inside Y, or anywhere else for that matter. Now let’s see how the behaviour of code is withing a single scope:

{
int a = 10;
a++; // we can refer to a, as it is in the same scope. We increment "a" by 1
printf("%d", a); // dont focus too much on this printf syntax, it just prints the "a" variable's value, which will print 11
int a = 15; // this fails completely (does not compile), because we cannot re-define the same variable within the same scope.
}

In the last line of code, where we re-define the variable “a”, the computer (compiler) will complain that we have two variables with the same name “a” within the same scope, which would cause ambiguity if executed, so it is not permissible.

But, we can declare a variable named “a” inside scope Y then there would be no conflict with scope X, as X and Y are different scopes and they do not know about each other. So, even though both “a” variables share the same name, they are actually different variables stored in different memory locations. All identifiers only exist within their respective scope. If we change the scope Y to:

{
int b = 10;
int a = 7;
// some code
}

It would be perfectly fine and the computer (compiler) would not complain at all. Variables that are declared within a scope are called local variables, and they are stored in the stack memory. We will later see that not all variables are stored in the stack memory. But first, let’s learn about the lifetime of variable.

Lifetime of a variable

Variables’ lifetimes are directly correlated with their scopes. Once the code execution flow exits a scope, all of the variables that were pushed to the stack are released, so you cannot use those variables beyond that point, because they actually do not exist anymore, it’s not a virtual restriction.

Global variables

Global variables are stored in a special memory segment, called the data segment. The data segment of a program basically contains global and static variables that will be alive throughout the execution of the program. They can be accessed within any scope, they are only released at the point of your program’s termination. We will later see more about how memory is released.

Now that you understand scopes more or less, you will have an easier time understanding if-statement bodies, loop bodies, function bodies and programming in general.

--

--

Muhamed Cicak

I like to read and learn more every day. I also like to teach. I am a software engineer that is here to both learn from others and contribute to the world.