Scope of Variables, C language Assignment Help

Assignment Help: >> Introduction to C language >> Scope of Variables, C language

Scope of Variable

 Scope of variable means, where the variable is available within the program. All variables may not necessarily be available to all statements in a program. There are two types of variables:-

Global Variable

These variables can be used in all functions of a 'c' program global variable are declared outside of all functions at the top of the program

Example-

 int a,b=6;

 main()

 {

                a=8;

                 ...............

                 ...............

                func();

 }

 func()

{

            int mult;

            mult=a*b;

            ...............

            ...............

}

Here a and b are the global variable.

 

Local Variables

These variables are local to specified function in which they declared that are not used by another function. Local variable are defined inside the function

Example

 func (i,j)

 int i,j;

 {

                int a,b;

                .............

 }

Here a and b are the local variables which are defined within the body of the function func( ). Local variable can be used for only that function, in which they are defined. The same variable name may be used in different functions and these variables are local to that function only.

Example-

var1(I,j)

int I,j;

{

            int a,b;

            a=5;

            b=5;

            .............

            .............

}

var2(m,n)

int m,n;

{

            int a,b;

            a=15;

            b=20;

            .............

            .............

}

Here value of a=5 and b=5 is local to the function var1 () and a=15, b=20 is local to the function var2 ().

 

External Variable

Where a global variable is declared in one file, but used by functions from another, then the variable is called an external variable in these functions, and must be declared as such. The declaration must be preceded by the word extern. The declaration is required so the compiler can find the type of the variable without having to search through several source files for the declaration. Global and external variables can be of any legal type. They can be initialized, but the initialization takes place when the program starts up, before entry to the main function.

 

Static Variable

Another class of local variable is the static type. A static can only be accessed from the function in which it was declared, like a local variable. The static variable is not destroyed on exit from the function; instead its value is preserved, and becomes available again when the function is next called. Static variables are declared as local variables, but the declaration is preceded by the word static.

Syntax: static int counter;

Static variables can be initialized as normal; the initialization is performed once only, when the program starts up.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd