GATE Exam | Aptitude Questions | GATE Syllabus | GATE Result | Mock Test | GATE Preparation
0 votes
Extern variables: belong to the External storage class and are stored in the main memory. extern is used when
we have to refer a function or variable that is implemented in other file in the same project. The scope of the
extern variables is Global.
Example:
/***************
Index: f1.c
****************/
#include <stdio.h>
extern int x;
int main() {
 printf("value of x %d", x);
 return 0;
}
Index: f2.c
****************/
int x = 3;
Here, the program written in file f1.c has the main function and reference to variable x. The file f2.c has the
declaration of variable x. The compiler should know the datatype of x and this is done by extern definition.

Global variables: are variables which are declared above the main( ) function. These variables are accessible
throughout the program. They can be accessed by all the functions in the program. Their default value is zero.
Example:
#include <stdio.h>
int x = 0;
/* Variable x is a global variable.
It can be accessed throughout the program */
void increment(void) {
 x = x + 1;
 printf("\n value of x: %d", x);
}
int main(){
 printf("\n value of x: %d", x);
 increment();
 return 0;
}
asked in Programming by anonymous

Related questions

The best answer to any question