data structure scope of variables

Post on 01-Dec-2014

1.043 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

Data Structure

Scope of Variables

Prepared By..Prepared By..Prepared By..Prepared By..

Saurav KumaSaurav KumaSaurav KumaSaurav Kumarrrr Shaambhavi PathakShaambhavi PathakShaambhavi PathakShaambhavi Pathak

B.B.B.B. tech CStech CStech CStech CS----OilOilOilOil &Gas Info&Gas Info&Gas Info&Gas Info B.B.B.B. tech CStech CStech CStech CS----OilOilOilOil &Gas Info&Gas Info&Gas Info&Gas Info

500016630500016630500016630500016630 500016891500016891500016891500016891

R970211046R970211046R970211046R970211046 R970211047R970211047R970211047R970211047

2011201120112011----2015201520152015 2011201120112011----2015201520152015

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 scopevariable scopevariable scopevariable scope.

There are different types of variable scope.

•Block ScopeBlock ScopeBlock ScopeBlock Scope

•Function ScopeFunction ScopeFunction ScopeFunction Scope

•Global Scope •Global Scope •Global Scope •Global Scope

•File Scope•File Scope•File Scope•File Scope

Now, let us Get down To each of them One by One.

Block ScopeBlock ScopeBlock ScopeBlock 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);

}

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);

}

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 ScopeFunction ScopeFunction ScopeFunction 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

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);

}

}

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);

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 ScopeGlobal ScopeGlobal ScopeGlobal 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.

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;

}

}

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);

}

The 1st output is 100 while the second output is 50.This example shows that block scope is valid

inside block (NNNNoteoteoteote: SAME NAME OF SAME NAME OF SAME NAME OF SAME NAME OF GLOBAL VARIABLE IS THEREGLOBAL VARIABLE IS THEREGLOBAL VARIABLE IS THEREGLOBAL VARIABLE IS THERE).

This Basically concludes the Global variables.

Now, before we begin with our next variable scope (file scope) we

must learn about variable storage classesvariable storage classesvariable storage classesvariable storage 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

•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.

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..!!

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

top related