Your SlideShare is downloading. ×
Data structure scope of variables
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.


Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Data structure scope of variables


Published on

Published in: Technology

  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. Data Structure Scope of Variables Prepared By.. KumarSaurav Kumar Shaambhavi Pathak CS-B. tech CS-Oil &Gas Info CS- B. tech CS-Oil &Gas Info500016630 500016891R970211046 R9702110472011-2011-2015 2011- 2011-2015
  • 2. In very simple words, scope of a variable can be defined as avalidity of a variable or other identifier within which itsdeclaration has an effect.A C program consists of multiple functions classes codestructures (like while, for, do-while loops).A Normal programmakes use of variables or other identifier under any functions formanipulation or storage purposes.Now, once the code is written it may or may not be accessible forthe other section of the code. This accessibility depends upon thedeclaration of the variables that how it was declared and where itwas declared. This comes under variable scope scope.There are different types of variable scope.•Block Scope Block•Function Scope Function•Global Scope•File ScopeNow, let us Get down To each of them One by One.
  • 3. Block ScopeC program is divided into many blocks of codes. They are mostly embraced ({}).Forexample, for loop has a statement block.It is Also Said to have a local scope.Look these codes:#include<stdio.h> //header filemain(){ 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 isnot 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 butnot outside.This Concludes the Block Scope.Function ScopeC program typically are structured with classes and codes calledfunction. Each function is capable of performing its ownprescribed task and takes in arguments and return a values andfurther depends on which type is it.Concept to know: Variable declared within a function isaccessible only under that function and those variables are calledlocal 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 functionthe program runs well…!!Thus we see that here in the function scope the local variableonce declared in one function is not accessible in any otherfunction.This concludes the basic of Function scope.Global ScopeA global scoped variable can be accessed through any Where inthe code irrespective of the function or file. It is usually placedjust 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 declarationvoid next(void); //function declarationmain(){ int a; for(a=0;a<=2;a++) { array[a]=a+1; //inserting values in globally created array }next(); //function callprintf(“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 anypiece of code.However, if a variable name is declared same in two different scopes(i.e global and block),then anychange made inside the block code for that variable will change the value of the variable for thatvariable inside the block. Same if any change is made outside the global variable’s value will bealtered..!!Let us Examine this concept through the following code:#include<stdio.h>int out=50; //global variablemain(){ { 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 validinside 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) wemust learn about variable storage classes classes.Sometimes we need to mention or declare the variables accordingto our programming needs. Variable storage class specifiers are used when declaring a variableto give the compiler information about how a variable is likely to beused and accessed within the program being compiled.•Static•Extern•auto•const
  • 12. •volatileHere we shall discuss only about static.Static as a Variable storage Class It specifies that a variable is to be accessible only within the scopeof current source file when it is declared globally. when it is declaredunder any function then it its value don’t die even after theexecution.This was just to introduce Static as a variable storage class.Now when static is either used with global variables or localvariables.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 programmeor even in another file.
  • 13. When we use static, this variable aquires file scope meaning thatthis 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 as1 1 1 1 1Then, you need to look at your work again..!!
  • 14. Due to the storage class static the value of m remainsconserved..!!So here is the output :This concludes the lesson.Sources: