C Programming - Declarations and Initializations - Discussion
Discussion Forum : Declarations and Initializations - Yes / No Questions (Q.No. 2)
2.
Suppose a program is divided into three files f1, f2 and f3, and a variable is defined in the file f1 but used in files f2 and f3. In such a case would we need the extern declaration for the variables in the files f2 and f3?
Discussion:
46 comments Page 4 of 5.
Riddhi said:
1 decade ago
Yes we have to declare extern here for multi use of variables because if we don't use extern then compiler gives error.
Hemanth Kumar M said:
1 decade ago
If 'a' is declared in file f1, To access 'a' in other files we have to declare 'a' as extern in other files. Otherwise it will show it as error.
Undefined 'a'. When a is declared as extern it will indicate that it is defined in other file and control starts to lookup for 'a' in other files.
Undefined 'a'. When a is declared as extern it will indicate that it is defined in other file and control starts to lookup for 'a' in other files.
Amandeep said:
10 years ago
As variable are used in file f2 and f3. So extern should be used for references.
Anson Cardoz said:
9 years ago
No variables defined in f1. So it should be declared as extern.
Pavan said:
9 years ago
No variables defined in f1 so it should be declared.
Priya said:
9 years ago
Without extern keyword we can not access from the variable with in outside file to inside (or) another file.
Sowmya said:
9 years ago
No, variables defined in f1 so it should be declared.
Thejas_Mysore said:
9 years ago
How the variable extern a at file_2 or file_3 is link with the variable a at file_1?
f1()
{
int a;
}
f2()
{
extern int a;
}
Where these two get linked?
f1()
{
int a;
}
f2()
{
extern int a;
}
Where these two get linked?
(2)
Farman said:
9 years ago
If any variable define in file f1 with extern keyword then it can be used in file f1, f2 because when we define any variable with extern then it will become atomic in the kernel so all atomic varible can be used in different different file.
Sneha said:
8 years ago
Variables are declared in f1 file. To use variables of f1 in f2&f3 extern keyword must be used.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers