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 1 of 5.
Renu said:
1 decade ago
Let us take that 'a' is the variable defined in the file f1 and used in f2 and f3 file for that we have to use the extern declaration because it's is defined in f1 file but if in file f2 & f3 their variable 'a' is defined then it will give priority to local variable and then their is no need for extern declaration.
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.
Ramya said:
1 decade ago
Yes we have to declare extern keyword otherwise we have to define the variable 'a' with in the function. Without declaration of extern keyword and define the variale if we compile we will get linked error.
Krishna said:
1 decade ago
How to include F1 file's variable in the other Files... With syntax... thanks
Akash said:
1 decade ago
We have to declare extern keyword otherwise we have to define the variable 'a' with in each function or else we have to declare 'a' as a global variable.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers