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 4 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.
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.
Makadiya dhara said:
1 decade ago
Without extern keyword we can not access the variable with in outside file to another file.
Divyapriya said:
1 decade ago
Yes, we need to declare a extern keyword variable in other files of program where we want to use it.
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.
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,
Sajla said:
1 decade ago
We can able to call the file by using extern keyword only because the file is external file.
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.
Krishna said:
1 decade ago
How to include F1 file's variable in the other Files... With syntax... thanks
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.
Rajvi said:
1 decade ago
Here, we have to declare variable globally. So both f2 & f3 can use that varible.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers