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

53.extern, register, Volatile, Constant variable

 

extern:

The visibility of global variable is limited to the portion after their declaration.

extern is a keyword used to declare the global or external variables to extend the visibility of global variables.

The keyword extern will not allocate the memory but only informs to the compiler that a global variable is declared and its visibility needs to be extended.

 

Example1:

#include<stdio.h>

void display();

void main()

{

   clrscr();

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

   display();

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

   getch();

}

int x=100;

void display()

{

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

   x=x+50;

}

 

Output:

Error: undefined symbol "x" in function main()  [ 2 times ]

 

 

Example explained:

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

 

Example rectified:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Output:

x=100

x=100

x=150

Example explained:

Generally visibility of "x" is limited to the function display()

Because "x" is declared as extern its visibility is extended to main() also.

 

Example2:

#include<stdio.h>

void first();

void second();

void main()

{

 

  extern int x;

  clrscr();

  first();

  second();

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

  getch();

}

 

void first()

{

   extern int x;

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

   x=x+10;

}

int x=10;

void second()

{

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

   x=x+10;

}

Output:

x=10

x=20

x=30

 

Example explained:

"x" is visible to only second() function

If we declare "extern int x;" before the main it is accessible into all the functions.

If we want to access "x" into required function then extern declaration can be done in the required function.

 

Example3:

extern can be applied even with the array but dimension must be specified.

 

#include<stdio.h>

void process();

void main()

{

   extern int x[];

   int i;

   clrscr();

   process();

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

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

   getch();

}

int x[]={12,45,67,87,23};

void process()

{

   int i;

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

      x[i]=x[i]+10;

}

 

Output:

22        55        77        97        33

 

register:

Register memory is a buffered memory between RAM and memory controller.

It is a high speed and expensive memory available in few bytes.

Memory allocation of normal variables allocates in the primary memory of computer.

By using the keyword register we can allocate the memory of a variable in register memory.

We can improve the performance of an application by allocating the frequently used variables in the register memory.

Example:

register int x;

 

Volatile:

It is a keyword used to declare a variable whose value would be assigned by some external source. The value may be changed time to time.

 

 

 

 

 

 

 

 

 


Constant variable:

const is the keyword used to declare the constant variable.

The value of which is initialized while its declaration.

The value of const variable can't be changed any where with the program.

const variables are mostly used to define the constants.

 

Example:

#include<stdio.h>

void main()

{

const float PI=3.14;

int rad;

float area,cir;

clrscr();

printf("Enter the radios:");

scanf("%d",&rad);

area=PI*rad*rad;

cir=2*PI*rad;

printf("Area of circle %f",area);

printf("\n Circumference of circle %f",cir);

getch();

}

 

Execution:

Enter the radios: 15

Area of circle: 706.500000

Circumference of circle: 94.200005

0 comments:

Post a Comment