Welcome !!

NICETECHVIDYA

Tutorials

Web Designs

You are welcome to Aptech Designers, designed by Pratik from Nepal.

Creativity is my passion. Logo describes your details about company.

Graphics Design needs vision to create yours unique digital concept.

Responsive and All Browser Compatible Dynamic Web Layouts our target.

Home Python C Language J A V A Our Courses
This WebSite is under Construction....

Friday, 11 March 2022

52.Global variables,Block variables,Storage classes,static

 

Global variables:

Variables declared outside all the functions are called global variables.

Visibility of these variables is along the program after their declaration.

Scope of these variables is along the program that is allocates at the beginning of program execution and de-allocates on completion of program execution.

 

Example1:

/* visibility of global variables */

#include<stdio.h>

int x=10;

void display();

void main()

{

  clrscr();

  printf("x=%d",x);

  display();

  printf("\nx=%d",x);

  getch();

}

void display()

{

   x=x+50;

}

 

Output:

x=10

x=60

 

Example explained:

Because "x" is a global variable it is accessed into both main() and display() functions.

 

Example2:

/* visibility of global variables */

#include<stdio.h>

void display();

void main()

{

   clrscr();

   printf("x=%d",x);

   display();

   printf("\nx=%d",x);

   getch();

}

int x=10;

void display()

{

   printf("\nx=%d",x);

   x=x+50;

}

 

Output:

Error: undefined symbol "x" in function main().

 

Example explained:

Though "x" is a global variable, its visibility is limited to the function display() can't be accessed into main().

 

 

 

 

 

 

Example3:

/* scope of global variables */

void display(int);

void main()

{

int i;

clrscr();

for(i=1;i<=5;i++)

    display(i);

getch();

}

int a=100;

void display(int x)

{

    a=a+x;

    printf("\t%d",a);

}

 

Output:

101      103      106      110      115

 

Example explained:

Because "a" is a global variable its scope is along the program.

The value of "a" is preserved for future iterations.

 

Block variables:

Variables defined within { } are called block variables.

A block { } may be exclusive or in association with any control structure.

The visibility of block variables is limited to the block in which they are declared.

Block variable can't be accessed out side the block.

When inner and outer block variables are with the same name then inner block variables override the outer block variables within the inner block.

 

Example1:

#include<stdio.h>

void main()

{

    int x=10;

    {

         int y=20;

         printf("y=%d",y);

     }   

     printf("\nx=%d",x);
     getch();

}

 

Output:

y=20

x=10

 

 

Example2:

#include<stdio.h>

void main()

{

    if(10<20)

    {

        int x=10;

        printf("x=%d",x);

    }

    printf("\nx=%d",x);

    getch();

}

 

Output:

Error: Undefined symbol "x" in function main()

 

Example explained:

Variable "x" is local to "if" block can't be accessed out side "if"

 

Example3:

#include<stdio.h>

void main()

{

    int x=40;

    if(10<20)

    {

        int x=10;   /* overriding the outer scope variable */

        printf("x=%d",x);

    }

    printf("\nx=%d",x); /* accessing outer scope variable */

    getch();

}

 

Output:

x=10

x=40

 

 

 

 

 

Storage classes:

Storage classes are the keywords used to control the memory allocation, visibility and scope of local and global variables.

These keywords are used with the variable declaration statements.

C language supports the following storage classes

1. auto

2. static

3. extern

4. register

5. volatile

 

auto:

It is the keyword used to declare the local variables of any function.

The scope (life) of these variables starts at the beginning of function execution and ends on completion of function execution.

Using of "auto" is optional in most of the latest compilers.

 

Example:

#include<stdio.h>

void main()

{

    auto int x=10,y=20;

    clrscr();

    printf("x=%d",x);

    printf("\ny=%d",y);

    getch();

}

 

Output:

x=10

y=20

                          

static:

It is the keyword used to declare only the local variables.

If a variable is not assigned with any value then it is initialized with some unknown value called garbage value but the static variable is initialized with zero (0) by default.

The scope of local variable is limited to the function in which it is declared but the scope of static variable is along the program.

 

Example1:

#include<stdio.h>

void main()

{

int x;

static int y;

clrscr();

printf("x=%d",x);

printf("\ny=%d",y);

getch();

}

 

Output:

x=62345 (Garbage value)

y=0

 

Example explained:

Because "x" is a non-static local variable, it is initialized with a garbage value.

Because "y" is a static variable, it is initialized with 0

 

Example2:

#include<stdio.h>

void display(int);

void main()

{

int i;

clrscr();

for(i=1;i<=5;i++)

    display(i);

getch();

}

void display(int x)

{

    int a=100;

    static int b=100;

    a=a+x;

    b=b+x;  

    printf("\n%d\t%d",a,b);

}

 

Output:

101      101

102      103

103      106

104      110

105      115

 

Example explained:

Both "a" and "b" are local variables to display ()

Because "a" is a non-static variable, its scope is limited to display (). Hence its value is not preserved for future iterations.

Because "b" is a static variable, its scope is along the program. Hence its value is preserver for future iterations.

Example3:

#include<stdio.h>

void main()

{

   static int x[5];

   int i;

   clrscr();

   for(i=0;i<5;i++)

      printf("%5d",x[i]);

   getch();

}

Output:

0          0          0          0          0

 

Example explained:

Even a static array is initialized with "0" data values

0 comments:

Post a Comment