Table of contents
Every variable in C has a defined scope. Scope refers to the accessibility or visibility of a variable at different points in a program.
Block Scope
A variable, which is declared within a statement block, ceases to exist as soon as the control exits the block. Such a "local" variable is said to have a block scope. E.g., a variable declared within a function is inaccessible outside of it.
N.B. In C, a set of statements enclosed within a pair of {}
is called a statement block.
What about nested blocks? ๐ค
Consider the following program containing a while
loop (inner block) within the main()
function (outer block):
#include<stdio.h>
int main()
{
int x = 10, i = 0;
printf("Value of x outside the loop: %d \n", x);
while (i < 2)
{
int x = 1;
printf("Value of x within the loop: %d \n", x);
i++;
}
printf("Value of x outside the loop: %d \n", x);
return 0;
}
/*
OUTPUT :
Value of x outside the loop: 10
Value of x within the loop: 1
Value of x within the loop: 1
Value of x outside the loop: 10
*/
Conclusions:
A variable (
x
in our case) defined within the inner block with the same name as that in the outer block masks the outer variable until the control exits the block. In essence, they're two different variables, occupying different memory locations.A variable declared in the outer block is accessible in the inner block, provided it's not re-declared.
Program Scope
We know that the variables declared within a function don't exist for any other function, but...
What if a function requires access to a variable that is not passed as a parameter? ๐ค
Global Variables.
Such variables are declared outside all the functions, even the main()
function, and are accessible from any point of the program.
Consider the following program:
#include<stdio.h>
int x = 10;
void print();
int main()
{
printf("Value of x in main(): %d\n", x);
int x = 2;
printf("Value of local x in main(): %d\n", x);
print();
return 0;
}
void print()
{
printf("Value of x in print(): %d\n", x);
}
/*
OUTPUT:
Value of x in main(): 10
Value of local x in main(): 2
Value of x in print(): 10
*/
Conclusion:
If a function has a local variable with the same name as that of a global variable, then the global will be ignored, and the local one will be considered during execution.
Storage Class: static
A storage class defines the lifespan of a variable. It specifies in which part of the memory the variable would be stored, and for how long.
static
variables are created at the beginning of program execution, their allocated memory space is freed only when the program terminates. E.g., global variables are static by-default.
static int x = 6;
(syntax to define a variable with static
storage class)
Consider the following program:
#include<stdio.h>
void print();
int main()
{
print();
printf("\n");
print();
return 0;
}
void print()
{
static int x = 1; // ATTENTION!
int y = 0;
printf("static variable x = %d\n", x);
printf("regular variable y = %d\n", y);
x++;
y++;
}
/*
OUTPUT:
static variable x = 1
regular variable y = 0
static variable x = 2
regular variable y = 0
*/
Conclusion:
A static
variable, when defined within a function, is not re-initialized every time the function called. It's initialized once, and further calls share the value from the last call.
N.B. Although static
variables exist even after the call stack vanishes, they have a block scope - not accessible outside the function.
Immensely grateful for your time ๐ค
Plzz like, comment, and share ๐
See ya ๐ฉต