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

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.

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

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

Arsh said:   1 decade ago
because only extern variables can be used in more than one file and to inform the compiler that these are declare some other file it needs to specify the keyword extern,

Sudhir said:   7 years ago
There are three files f1 f2 and f3 which are part of the single program not functions. How they are separated and how they are linked to each other? Explain me.
(1)

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.

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.

Manasa said:   1 decade ago
We will use the extern keyword for f1 and when we use it for f1 no need of defining the keyword extern before f2 and f3 values can access the f1.

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.

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


Post your comments here:

Your comments will be displayed after verification.