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 3 of 5.
Sneha said:
8 years ago
Variables are declared in f1 file. To use variables of f1 in f2&f3 extern keyword must be used.
Shivashish Bagora said:
7 years ago
Yes, definitely if you declare a variable of extern storage class. That means it has a global variable and it can use throughout the code in another file also.
Pooja said:
7 years ago
Yes, but we have to declare that variable globally in f1 file also.
Saravana said:
1 decade ago
If the sub functions f1 and f2 share the function f1 datatypes, no need to specify again the datatypes, otherwise we have to mention extern.
Sunil Kumar(S.K) said:
1 decade ago
If we want to use a variable which is defined in another file than it is must that we declare that variable is as extern. If we do not do this than compiler error occur (undefined variable).
VIJAY said:
1 decade ago
We must use extern because we access the value from the external file F1, So we use extern.
Knight said:
1 decade ago
The variable declared by using keyword extern point to a memory location which is already defined. If that variable is not defined somewhere else it will leads to a compiler error "undefined reference to a memory location"
Susi said:
1 decade ago
Without extern keyword we can not access from the variable with in outside file to inside (or) another file.
Sowma VN said:
1 decade ago
Yes we have to declare extern keyword without that we can't access for both f2 and f3 we have to declare.
Rajvi said:
1 decade ago
Here, we have to declare variable globally. So both f2 & f3 can use that varible.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers