Scope of Variables in C++2 min read

In any programming language, the scope is a region which is can be defined as the extent up to which something is valid. All the variables used in a program have their extent of use and if used out of that extent or boundary, they do not hold their values. Hence this boundary is referred to as the scope of a variable

These boundaries are referred to curly braces {} in a program. Based on their scope, there are two types of variables.

  • Local variables: inside a function or a block.
  • Global variables: outside of all functions.
  • Formal parameters: function parameters.
  • block: refers to anything between ‘{‘ and ‘}’.

1. Local variables:

Local variables are the variables that are declared within the body of the function or block. These types of variables can only be used within a block where it is created or declared, it has no existence outside the block.

Example: C++ example using local variables.

Output:

Let us see one more example where the variable with same name can be used in different function holding different values.

Output:


2. Global Variable

These variables are available to use throughout the program that is they have their scope throughout the program. They are defined outside the function (including main()), usually, you will see them at the top of any program.

After its declaration in a program, a global variable can be used inside any function present in a program.

Example: C++ example using global variables.

Output:

Let us see one more example where the local variable takes preference.

Output:

As you can see in the above program, g is declared global with a value of 20, but again it is initialized inside testFunction() with the value of 30. And present value(30) overrides the previous value (20) and prints 30.

In the main function again g is defined and initialized (making it local variable) with 50 that overrides the 20.


MORE

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More

Inverted Half Pyramid Pattern of Alphabets in C

In this tutorial, we will write a C program to print half Pyramid using alphabets/characters. Before that, you may go through the following topic in …
Read More