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 3 of 5.

Gaurav saini said:   1 decade ago
Yes Extern keyword is to be used while we define the variable in file f1 so as to make this same variable accessible in the file f2 and f3. Since not using extern will make the variable local inside the file f1. And that time for it act like global variable.

Supriya said:   1 decade ago
Yes we need to specify the extern because we are taking the data from the other file.

Phantom said:   1 decade ago
Yes Extern keyword is to be used while we define the variable in file f1 so as to make this same variable accessible in the file f2 and f3. Since not using extern will make the variable local inside the file f1.

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.

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;
}

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

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'.

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.

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

Renu said:   1 decade ago
Yes, we need to declare a extern keyword variable in other files of program where we want to use it.


Post your comments here:

Your comments will be displayed after verification.