Understanding Local and Global Variables in C

For Complete YouTube Video: Click Here

In this class, we will have Understanding Local and Global Variables in C

We have already discussed functions in our previous classes.

Local and Global Variables

Local Variables

Local Variables: A variable declared in the body of a function is said to be local to the ‘function.’

Global Variables

Global Variables: Variables that are declared outside the body of a function are called Global Variables.

Every variable in the program will have a particular scope of existence in a program.

Scope of a variable

Scope of a Variable: is the portion of the program text in which we can reference the variables.

Block Scope: Variable visible from the point of declaration to the end of the enclosing function. Local Variables have a block scope.

File Scope: Variables visible from its point of declaration to the end of the file enclosing file. Global Variables have file scope.

Example on Local and Global Variables

The image below is an example of local and global variables.

Understanding Local and Global Variables in C Example
Understanding Local and Global Variables in C Example

In the above example, the ‘main’ function is having a local variable mainlocalvar.

The scope of mainlocalvar is block scope.

Similarly, the fun function has a local variable funlocalvar.

The scope of funlocalvar is block scope.

Once the program’s execution comes out of the fun function, we can not reference that variable.

The reference of the variable can be made within the block of the function.

Above the ‘main’ function, we have a global variable declaration.

So far, the declaration of a variable is seen within the function.

In C, we can declare the variable above the ‘main’ function.

Such kinds of declarations are global declarations.

The scope of a global variable is file scope.

Any functions in the program can access these variables.

Understanding Local and Global variables using the process of a program

The image below is the process of a program.

Understanding Local and Global Variables in C Process for a Program Process
Understanding Process for a Program Process

We have already discussed the process of a program.

Now, based upon that discussion, we will discuss the above program.

In the above image, the variable iamglobalvar will be stored in the static and global variables segment of the process.

The image below is the activation record for the ‘main’ function call.

Understanding Local and Global Variables in C main Record
Understanding Local and Global Variables in C main Record

The local variables of the ‘main’ function are created in the activation record.

Any function can access the global variables in the static and global variables segment till the end of the program’s execution.

Within the ‘main’ function call, the fun function call is made.

The activation record for the fun function is shown in the image below.

Understanding Local and Global Variables in C fun Record
Understanding Local and Global Variables in C fun Record

As the program’s execution is in the fun function activation record, we can not access the mainlocalvar.

The fun function call has access only to its local variable funlocalvar and to the global variable iamglobalvar.

As the execution of the fun function completes, the reference to the funlocalvar is not possible.