The extern keyword in C is utilized for declaring external variables. This modifier is applicable to various data types, including int, float, double, arrays, pointers, structures, functions, and others. It signifies that the variable is declared but not defined in the current scope and will be provided by another source during linking.
Important points about the extern
keyword:
- 1. It serves as the default storage class for all global variables and functions.
For example, Analyze following two c code and its output:
(a)
#include <stdio.h>
int i; //By default it is extern variable
int main(){
printf("%d",i);
return 0;
}
Output: 0
(b)
#include <stdio.h>
extern int i; //extern variable
int main(){
printf("%d",i);
return 0;
}
Output: Compilation error - Undefined symbol 'i'.
Question: In both programs, the variable i
is declared as an extern variable. However, why is the output different? Please refer to the second and third points for clarification.
(c)
#include <stdio.h>
void sum(int,int) //By default it is extern.
int main(){
int a=5,b=10;
sum(a,b);
return 0;
}
void sum(int a,int b){
printf("%d”",a+b);
}
Output: 15
2When we use the extern
modifier with any variables, it serves as a declaration only, indicating that the variable is defined elsewhere. In the second case, the compiler is showing an error for an unknown symbol i
because the memory is not allocated for this extern variable in the current scope. To define a variable, i.e., to allocate memory for extern variables, it is necessary to initialize them.
In essence, using extern
informs the compiler that the variable is declared elsewhere, and the actual memory allocation and definition will be provided in another part of the program or in a different file during linking. For example:
#include <stdio.h>
extern int i=10; //extern variable
int main(){
printf("%d",i);
return 0;
}
Output: 10
3. If you do not use the extern
keyword with global variables, the compiler will automatically initialize the extern variable with its default value.
4. The default initial value of an extern integral type variable is zero, or null for other types.
For example:
#include <stdio.h>
char c;
int i;
float f;
char *str;
int main(){
printf("%d %d %f %s",c,i,f,str);
return 0;
}
Output: 0 0 0.000000 (null)
5. We cannot initialize extern variable locally i.e. within any block either at the time of declaration or separately. We can only initialize extern variable globally. For example:
(a)
#include <stdio.h>
int main(){
extern int i=10; //Try to initialize extern variable
//locally.
printf("%d",i);
return 0;
}
Output: Compilation error: Cannot initialize extern variable.
(b)
#include <stdio.h>
int main(){
extern int i; //Declaration of extern variable i.
int i=10; //Try to locally initialization of
//extern variable i.
printf("%d",i);
return 0;
}
Output: Compilation error - Multiple declarations of variable 'i'.
6. If a variable is declared as an extern variable, the compiler searches for its initialization. If it is initialized, which can be either extern or static, it is acceptable. Otherwise, the compiler will generate an error. For example:
(a)
#include <stdio.h>
int main(){
extern int i; //It will search the initialization of
//variable i.
printf("%d",i);
return 0;
}
int i=20; //Initialization of variable i.
Output: 20
(b)
#include <stdio.h>
int main(){
extern int i; //It will search the any initialized
//variable i which may be static or
//extern.
printf("%d",i);
return 0;
}
extern int i=20; //Initialization of extern variable i.
Output: 20
(c)
#include <stdio.h>
int main(){
extern int i; //It will search the any initialized
//variable i which may be static or
//extern.
printf("%d",i);
return 0;
}
static int i=20; //Initialization of static variable i.
Output: 20
(d)
#include <stdio.h>
int main(){
extern int i; //variable i has declared but not
//initialized
printf("%d",i);
return 0;
}
Output: Compilation error- Unknown symbol i.
7. A particular extern variable can be declared multiple times, but it can only be initialized once. For example:
(a)
extern int i; //Declaring the variable i.
int i=25; //Initializing the variable.
extern int i; //Again declaring the variable i.
#include <stdio.h>
int main(){
extern int i; //Again declaring the variable i.
printf("%d",i);
return 0;
}
Output: 25
(b)
extern int i; //Declaring the variable
int i=25; //Initializing the variable
#include <stdio.h>
int main(){
printf("%d",i);
return 0;
}
int i=20; //Initializing the variable
Output: Compilation error - Multiple initialization of variable 'i'..
8. We cannot write any assignment statement globally. For example:
#include <stdio.h>
extern int i;
int i=10; //Initialization statement
i=25; //Assignment statement
int main(){
printf("%d",i);
return 0;
}
Output: Compilation error
Assigning a value to a variable at the time of declaration is known as initialization, while assigning a value to a variable not at the time of declaration is known as assignment.
(b)
#include <stdio.h>
extern int i;
int main(){
i=25; //Assignment statement
printf("%d",i);
return 0;
}
int i=10; //Initialization statement
Output: 25
9. If a variable or function is declared as extern globally, its visibility extends throughout the entire program, whether it consists of a single file or multiple files. For example consider a c program which has written in two files named as one.c and two.c:
(a)
//one.c
#include<conio.h>
int i=25; //By default extern variable
int j=5; //By default extern variable
/**
Above two lines is initialization of variable i and j.
*/
void main(){
clrscr();
sum();
getch();
}
//two.c
#include<stdio.h>
extern int i; //Declaration of variable i.
extern int j; //Declaration of variable j.
/**
The two lines above will search for the initialization statements of variables i and j. If the initialized variable is declared as static or extern, the search will extend to "two.c"; if it's declared as extern, the search will extend to "one.c".
*/
void sum(){
int s;
s=i+j;
printf("%d",s);
}
Compile and execute above two file one.c and two.c at the same time:
In Turbo c compiler
Step 1: Create two files named one.c
and two.c
(or any preferred names) and write the respective codes in each file. Save the files.
Step 2:
- Open Turbo C++ IDE.
- Click on the "Project" menu.
- Select "Open Project" from the dropdown menu.
This will allow you to open the project where you have saved your one.c
and two.c
files.
Step 3: Upon selecting "Open Project," you will be presented with the following screen.
In the "Open Project" dialog, enter any project name with a .prj extension in the "File" text field. For example, I am using the project name "CProject.PRJ." After entering the name, click the OK button.
Step 4: Upon selecting "Open Project," you will be presented with the following screen.
Now, navigate to the "Project" menu and select "Add item."
Step 5: After clicking "Add item," you will see the following screen.
In the "Name" text field, enter each C source code file name one by one. For example, start by entering "one.c," then click the "Add" button. Continue this process for each file, such as "two.c," clicking "Add" after each entry. Repeat until you have added all the relevant source code files.
Step 6: Finally, click on the "Done" button. After clicking "Done," you will see the following screen.
At the lower part of the window, you will find information about the project, including its name and the list of files you have added.
Step 7: To compile the two files, press Alt+F9, and to run the program, press Ctrl+F9.
Note: To close the project click on Project -> Close project.
Output: 30
Hence, we can affirm that variables i and j, which have been initialized in two.c, are also visible in the file one.c. This example demonstrates the visibility of globally declared extern variables across the entire program.
Note: In the above example, the function sum
, which was declared and defined in two.c
, also has the storage class extern
. Therefore, we can call this function from another file (one.c
). If it were declared as static
, we wouldn't be able to call the sum
function because the static
storage class restricts visibility to the file in which it is declared.
10. Extern variables or functions have external linkage, making them visible to all files.
4 comments:
Hi,
Please elaborate the answer of the question 7.
Regards,
Anand
Very Good solving for Interviews
Plaese give correct explanation of question 15 plzzz
Please Correct 6th example of Question 6
given :
unsigned **(*(*ptr)[8](char const *, ...)
correct :
unsigned **( *(*ptr)[8] ) (char const *, ...)
')' is missing after [8]
Post a Comment