REGISTER VARIABLES and EXTERNAL VARIABLES

 Using keyword register we can declare a register variable. Ex: register int flag = 1; Register variables are stored in high-speed registers. Register variables Can only be used as automatic variables. Register variables are not stored in memory so they do not have address. NO POINTER can hold an address of register variable. NO Default value exist for a Register variable i.e it possess a JUNK value when uninitialized.

Program to illustrate use of register variable.

#include <stdio.h>
void main()
{
int a, b;
register int z;
printf("Enters two number to add \n");
scanf("%d %d", &a, &b);
z=a+b;
printf("\n sum=%d", z);
}

EXTERNAL VARIABLES

 Specifies actual storage and initial value of a variable or Function body is defined elsewhere. The keyword extern is used to declare/define an Extern / Global variables Ex: extern int x, y; The default scope of variables or function declared/defined outside of all functions is Global. Global variables exist for entire program execution and can be accessed by Every function. By Default global variable hold ZERO value. In case of large program, containing more than one file, if the global variable is declared in file 1 and that variable is used in file 2 then, compiler will show error. To solve this problem, keyword extern is used in file 2 to indicate that, the variable specified is global variable and declared in another file.

Program to demonstrate working local and global variables.

#include <stdio.h>
int z; // z is a global variable
void display()
{
printf("\n sum=%d", z);
}
void main()
{ // a and b are local variables
auto int a, b; //or int a, b;
printf("Enters two number to add: ");
scanf("%d %d", &a, &b);
z=a+b;
display();
}
REGISTER VARIABLES and EXTERNAL VARIABLES REGISTER VARIABLES and EXTERNAL VARIABLES Reviewed by Unknown on 04:45 Rating: 5

No comments:

Powered by Blogger.