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?
Yes
No
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
46 comments Page 5 of 5.

Pavan kumar said:   1 decade ago
We want to declare the variable extern in file f1 or f2?

Tuks said:   1 decade ago
Declaration of global variables should not be within any fun.

It should be out of all fun i.e. before main.

RohitN said:   1 decade ago
In C every variable or function which is defined in a file other than the one in which it is getting used, is declared as 'extern'.

Praveen IIIT said:   1 decade ago
Yes, If you want access Programs in other files, you must declare a extern keyword.

Mohan said:   1 decade ago
Yes with you extern declaration , we cannot use the variable in f2 , f3 which was declared in the f1.

f1()
{
variable v1;
}

Extern f2()
{
used v1;
}

Extern f3()
{
used v1;
}

Aarthi said:   1 decade ago
Yes, because a variable declared in one file is local to it. Therefore in order to access that variable in another file it must be given the correct access authority. Therefore Extern keyword is used.


Post your comments here:

Your comments will be displayed after verification.