C Programming - Declarations and Initializations - Discussion
Discussion Forum : Declarations and Initializations - General Questions (Q.No. 4)
4.
Is there any difference between following declarations?
1 : | extern int fun(); |
2 : | int fun(); |
Answer: Option
Explanation:
extern int fun(); declaration in C is to indicate the existence of a global function and it is defined externally to the current module or in another file.
int fun(); declaration in C is to indicate the existence of a function inside the current module or in the same file.
Discussion:
56 comments Page 2 of 6.
Wikiok said:
1 decade ago
@Nancy: http://wiki.answers.com/Q/What_is_the_use_of_extern_in_C
"Declaring a variable as extern will result in your program not reserving any memory for the variable in the scope that it was declared."
"Declaring a variable as extern will result in your program not reserving any memory for the variable in the scope that it was declared."
Ganesh said:
1 decade ago
Hi sir you said extern is globally declared where as int fun you said localy declared so how you can so it is not different.
Hasi said:
1 decade ago
Global function is nothing but the variables which are used for overall program. Where as local variables are used in specific function or a specific part of the program.
Prakash said:
1 decade ago
Then 'extern' keyword not understand, please explain.
Sanket V Garg said:
1 decade ago
If we declare any functions external as well as an internal function in same code then if latter that function is called from anywhere in file the which function will be loaded? how is the issue of function resolved in these scenario? can anyone please explain these to me?
Manas said:
1 decade ago
The extern keyword means "declare without defining". In other words, it is a way to explicitly declare a variable, or to force a declaration without a definition. It is also possible to explicitly define a variable, i.e. to force a definition. It is done by assigning an initialization value to a variable. If neither the extern keyword nor an initialization value are present, the statement can be either a declaration or a definition. It is up to the compiler to analyse the modules of the program and decide.
Venkatramulu said:
1 decade ago
I didn't got suitable answer for "extern" keyword can any one explain with example? please ?
Khusi said:
1 decade ago
If we declare any functions external as well as an internal function in same code then if latter that function is called from anywhere in file the which function will be loaded? how is the issue of function resolved in these scenario? can anyone please explain these to me?
Lucky said:
1 decade ago
File 1:
int GlobalVariable; // implicit definition
void SomeFunction(); // function prototype (declaration)
int main() {
GlobalVariable = 1;
SomeFunction();
return 0;
}
File 2:
extern int GlobalVariable; // explicit declaration
void SomeFunction() { // function header (definition)
++GlobalVariable;
}
In this example, the variable GlobalVariable is defined in File 1. In order to utilize the same variable in File 2, it must be declared. Regardless of the number of files, a global variable is only defined once, however, it must be declared in any file outside of the one containing the definition.
If the program is in several source files, and a variable is defined in file1 and used in file2 and file3, then extern declarations are needed in file2 and file3 to connect the occurrences of the variable. The usual practice is to collect extern declarations of variables and functions in a separate file, historically called a header, that is included by #include at the front of each source file. The suffix .h is conventional for header names
int GlobalVariable; // implicit definition
void SomeFunction(); // function prototype (declaration)
int main() {
GlobalVariable = 1;
SomeFunction();
return 0;
}
File 2:
extern int GlobalVariable; // explicit declaration
void SomeFunction() { // function header (definition)
++GlobalVariable;
}
In this example, the variable GlobalVariable is defined in File 1. In order to utilize the same variable in File 2, it must be declared. Regardless of the number of files, a global variable is only defined once, however, it must be declared in any file outside of the one containing the definition.
If the program is in several source files, and a variable is defined in file1 and used in file2 and file3, then extern declarations are needed in file2 and file3 to connect the occurrences of the variable. The usual practice is to collect extern declarations of variables and functions in a separate file, historically called a header, that is included by #include at the front of each source file. The suffix .h is conventional for header names
Ganesh said:
1 decade ago
When source code is compiled, it is turned into "object code." Object code is a medium between the final executable and the original source code.
In object code, everything that can be accessed from another object code is given a name, referred to as a "symbol." For example, functions and global variables have symbols. The object code can also depend on symbols it doesn't contain. For example, it can call a function that isn't defined in the source code it was compiled from.
After all the objects have been compiled, they are sent to a linker that links the objects into a single executable. The linker makes sure that all the symbol dependencies are met. (In other words, it makes sure that, if one of the objects calls a function called "foo," then "foo" is defined in one of the objects or a library. Libraries are collections of symbols and their definitions.)
The extern keyword is to tell the compiler that the variable is defined somewhere else, and that the compiler should make that variable a dependency.
In object code, everything that can be accessed from another object code is given a name, referred to as a "symbol." For example, functions and global variables have symbols. The object code can also depend on symbols it doesn't contain. For example, it can call a function that isn't defined in the source code it was compiled from.
After all the objects have been compiled, they are sent to a linker that links the objects into a single executable. The linker makes sure that all the symbol dependencies are met. (In other words, it makes sure that, if one of the objects calls a function called "foo," then "foo" is defined in one of the objects or a library. Libraries are collections of symbols and their definitions.)
The extern keyword is to tell the compiler that the variable is defined somewhere else, and that the compiler should make that variable a dependency.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers