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

Thiruneelakandar.s said:   1 decade ago
Global variable declaration is we can access to any of the function or files.

Want to know more details down to follow here.

First up all we want to know about data types or storage class.

There are 4 classes.

1. Local variable.
2. Static variable.
3. Global variable.
4. Register variable.

Local variable only to use a function. We cannot access other function and life is a function only.

Static variable is one or more function to use, the static variable is update automatically.

Global variable is declaration the variable is out side the function, so we can use the variable in any of the function.

Register variable declaration only internal the CPU memory.

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.

Hemanth Kumar M said:   1 decade ago
If 'a' is declared in file f1, To access 'a' in other files we have to declare 'a' as extern in other files. Otherwise it will show it as error.

Undefined 'a'. When a is declared as extern it will indicate that it is defined in other file and control starts to lookup for 'a' in other files.

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.

Farman said:   9 years ago
If any variable define in file f1 with extern keyword then it can be used in file f1, f2 because when we define any variable with extern then it will become atomic in the kernel so all atomic varible can be used in different different file.

Shubham sharnma said:   1 decade ago
If we declare extern keyword then translation unit of all program will connect all f1, f2, f3 files if we not do this then translation unit will limited to one source file and the linkage between all files will not connected internally.

Thejas_Mysore said:   9 years ago
How the variable extern a at file_2 or file_3 is link with the variable a at file_1?

f1()
{
int a;
}

f2()
{
extern int a;
}

Where these two get linked?
(2)

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"

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.

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.


Post your comments here:

Your comments will be displayed after verification.