#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Data structure scope of variables
1. Data Structure
Scope of Variables
Prepared By..
Kumar
Saurav Kumar Shaambhavi Pathak
CS-
B. tech CS-Oil &Gas Info CS-
B. tech CS-Oil &Gas Info
500016630 500016891
R970211046 R970211047
2011-
2011-2015 2011-
2011-2015
2. In very simple words, scope of a variable can be defined as a
validity of a variable or other identifier within which its
declaration has an effect.
A C program consists of multiple functions classes code
structures (like while, for, do-while loops).A Normal program
makes use of variables or other identifier under any functions for
manipulation or storage purposes.
Now, once the code is written it may or may not be accessible for
the other section of the code. This accessibility depends upon the
declaration of the variables that how it was declared and where it
was declared. This comes under variable scope
scope.
There are different types of variable scope.
•Block Scope
Block
•Function Scope
Function
•Global Scope
•File Scope
Now, let us Get down To each of them One by One.
3. Block Scope
C program is divided into many blocks of codes. They are mostly embraced ({}).For
example, for loop has a statement block.
It is Also Said to have a local scope.
Look these codes:
#include<stdio.h> //header file
main()
{
int i; //local variable
for(i=0;i<5;i++)
{
int j=i*i;
printf(“nValue of j is %d”,j);
}
//printf(“nValue of j is %d”,j);
}
4. As seen from the output screen that if the comment statement is
not executed the program runs well in code blocks IDE.
Now,The Difference lies here…
#include<stdio.h>
main()
{
int i;
for(i=0;i<5;i++)
{
int j=i*i;
printf(“nValue of j is %d”,j);
}
printf(“nValue of j is %d”,j);
}
5. Follows by an error..!!
The explanation behind this is that the variable j is only valid inside the for loop but
not outside.
This Concludes the Block Scope.
Function Scope
C program typically are structured with classes and codes called
function. Each function is capable of performing its own
prescribed task and takes in arguments and return a values and
further depends on which type is it.
Concept to know: Variable declared within a function is
accessible only under that function and those variables are called
local variables.
Look to this piece of code:
#include<stdio.h>
int fact(void); //function declaration
6. main()
{
int n,res;
printf(“nEnter a no “);
scanf(“%d”,&n);
res=fact(n);
printf(“nValue is %d”,res);
}
int fact (void)
{
int res; //local variable in fact function
if(n!=1)
{
res=n*fact(n-1);
return(res);
}
else
{
return(1);
}
}
7. Any Guess What the error would be..??
Its due to that we are neglecting the the function scope.
Lets try Improving our code..
#include<stdio.h>
int fact(int);
main()
{
int n,res;
printf(“nEnter a no “);
scanf(“%d”,&n);
res=fact(n); //function call
printf(“nValue is %d”,res);
}
int fact (int n)
{
int res;
if(n!=1)
{
res=n*fact(n-1);
8. return(res);
}
else
{
return(1);
}}
Now when we Corrected/declared local variable in fact function
the program runs well…!!
Thus we see that here in the function scope the local variable
once declared in one function is not accessible in any other
function.
This concludes the basic of Function scope.
Global Scope
A global scoped variable can be accessed through any Where in
the code irrespective of the function or file. It is usually placed
just below the source file.
It is also said to have a Program Scope.
9. Let us examine this code:
#include<stdio.h>
int array[5]; //global array declaration
void next(void); //function declaration
main()
{
int a;
for(a=0;a<=2;a++)
{
array[a]=a+1; //inserting values in globally created array
}
next(); //function call
printf(“n”);
for(a=0;a<5;a++) //loop for printing the stroed values in the array
{
printf(“t%d”,array[a]);
}
}
void next(void)
{
int b;
for(b=3;b<=4;b++)
{
array[b]=b+1;
}
10. }
Hence From the above output it clearly shows that the global variable can be accessed throught any
piece of code.
However, if a variable name is declared same in two different scopes(i.e global and block),then any
change made inside the block code for that variable will change the value of the variable for that
variable inside the block. Same if any change is made outside the global variable’s value will be
altered..!!
Let us Examine this concept through the following code:
#include<stdio.h>
int out=50; //global variable
main()
{
{
int out=50; //local variable
out=out+50;
printf("nTha value of variable out inside the block is %d",out);
}
printf("nThe value of out variable outside the block is %d",out);
}
11. The 1st output is 100 while the second output is 50.This example shows that block scope is valid
inside block (Note SAME NAME OF GLOBAL VARIABLE IS THERE
Note: THERE).
This Basically concludes the Global variables.
Now, before we begin with our next variable scope (file scope) we
must learn about variable storage classes
classes.
Sometimes we need to mention or declare the variables according
to our programming needs.
Variable storage class specifiers are used when declaring a variable
to give the compiler information about how a variable is likely to be
used and accessed within the program being compiled.
•Static
•Extern
•auto
•const
12. •volatile
Here we shall discuss only about static.
Static as a Variable storage Class
It specifies that a variable is to be accessible only within the scope
of current source file when it is declared globally. when it is declared
under any function then it its value don’t die even after the
execution.
This was just to introduce Static as a variable storage class.
Now when static is either used with global variables or local
variables.
Let us see an example of static used with Global variable.
#include<stdio.h>
static int marks=100; //static global variable
main()
{
//statements
}
Initially global variables could be used anywhere in the programme
or even in another file.
13. When we use static, this variable aquires file scope meaning that
this variable is valid only in the current file and not in any other file.
Now let us see an example of static used with local variable..
#include<stdio.h>
void fun();
main()
{
int i;
for(i=0;i<5;i++)
{
fun();
}
}
void fun()
{
static m=0;
m++;
printf("t%d",m);
}
If you got output as
1 1 1 1 1
Then, you need to look at your work again..!!
14. Due to the storage class static the value of m remains
conserved..!!
So here is the output :
This concludes the lesson.
Sources: http://www.learncpp.com/cpp-tutorial/43-file-scope-and-the-static-keyword/
http://www.techotopia.com/index.php/Objective-C_Variable_Scope_and_Storage_Class
http://icecube.wisc.edu/~dglo/c_class/scope.html