Scope of Global and Static variables in C/C++

The scope of a variable means where that specific variable could be used. It means the inside the specific file or the files which are including that files. Global and static variables vary from each other only by their scope. There is no other prominent difference in these variables.

Variables life time

Let’s first make one thing clear, that scope of a variable is NOT its lifetime. Lifetime, however is something else. Lifetime of a static variable is the entire run of the program in which it is defined. Static variables are used when we are referring to a common property of all the objects, means which is not different for different objects like the country of a number of employees, it will be same for all the employees but will need in all employee properties.

Static variables Introduction

A static variable is declared inside a function or a block, and it can be called or used only in that specific function block. However, as the name specifies, the value of the static variable remains persistent.

Static Variables Example

The example code give below explains the use of static variables.

#include<stdio.h>
void static_fun()
{
	static int var = 10;
	printf("The value of static variable is %d\n", var++);
}
int main (void)
{
	static_fun();
	static_fun();
	static_fun();
}

The output of this code sample will explain the use of static variable itself

Output:

Scope of global and static variable in c programming program output static

Every time the function static_fun() was called it retains the previous value of the static variable. The main reason for this is that the static variable is stored in the data segment of memory instead of the stack at the time function call.

Now lets change the above code by removing static from variable var like this and see the output:

#include<stdio.h>
void static_fun()
{
	int var = 10;
	printf("The value of static variable is %d\n", var++);
}
int main (void)
{
	static_fun();
	static_fun();
	static_fun();
}

If var was a normal variable instead of static the output will be 10 at every function call as shown in the figure below.

The value of static variable is 10                                                            
The value of static variable is 10                                                            
The value of static variable is 10

The scope of this static variable is restricted to the function block in which it is declared, any call to this variable from outside of this block will result in an error.

Global variables Introuduction

A global variable as the name suggests will be declared outside of all the functions and will be accessible by not only all the functions present in that file but also by all the files which include that specific file in which the global variable was declared. On the contrary, if we declare a static variable outside of all the functions, it will be accessible only in the functions of that file and not from the files including that file. A simple example below will explain the use of global variables.

Global variable Demo Program

This variable var is declared outside of all the function and hence can be used anywhere in this file and its value is also persistent as we can see from the output of the above code.

#include<stdio.h>
int var = 10;
void global_fun_1()
{
 printf("The value of global variable is %d\n", var++);
}
void global_fun_2()
{
printf("The value of global variable is %d\n", var++);
}

void global_fun_3()
{
printf("The value of global variable is %d\n", var++);
}

int main (void)
{
global_fun_1();
global_fun_1();
global_fun_1();
global_fun_2();
global_fun_2();
global_fun_3();
}

Output:

Scope of global and static variable in c programmin global variable demo program output
  1. C Program Compilation steps & Example with GCC Linux
  2. Introduction to Pointers
  3. How to find an array size without using sizeof()
  4. Pointer Arithmetic, Pointer Size, and Pointer Type
  5. In which order function parameters are evaluated in C/C++?
  6. Scope of global and static variables in C/C++

Leave a Reply