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();
Both are identical
No difference, except extern int fun(); is probably in another file
int fun(); is overrided with extern int fun();
None of these
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 1 of 6.

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

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.

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.

PRIYANKA CHAVAN said:   10 years ago
In C there are four storage classes.

1) Automatic = Auto, its lifetime is within the fun. By default it is garbage initialization.

2) Static = Life cycle of static is throughout the program, default value is 0.

3) Extern = It is global variable. Outside the function, treated as global variable only.

Ex:

int a;(global variable)
main()
{
a=4; (local variable)
printf(a);
mdify();
printf(a);
}
modify()
{
a=a+1;
printf(a)
}

Hope it is clear.

Venkat said:   8 years ago
@ALL.

extern is keyword .

When we use this word before the variable then that will store in the stack.
extern int a;
main()
{
-----
-----
}

In above program, I declared but not assigned so "a" value may present in other programs then you will get that value by using extern without this we can not get it.

The same way when function declares with extern we can get from other programs. But our compiler assumes that if you do not mention extern (in function concept only) it treats as extern.
(2)

Keerthi said:   8 years ago
Extern is a data storage type or storage class.
extern is a global variable.
Scope of the global variable is global.

Eg:-
file1.c

include "file2.c"
main()
{
extern int a;
printf(",,...d",a);
}


file2.c

int a;

extern points to the int a of file2.c(another file) .
Because file1.c refers to file2.c (#include "file2.c" )
(1)

Venkatesh said:   8 months ago
@All.

Here is my explanation.
1) extern and global are the same thing but when we use the keyword "extern" is important.
2) extern use only for function and variable and use when you assign an extern then don't assign value it raises an error.
3) The directly say that use on extern is you access the variable or function inside as well as outside file.

Adithya said:   1 decade ago
Oh horrible explanation. By declaring a variable as an extern, you just tell the compiler that a variable has been declared. Without an extern, something like this:

int x;

The compiler declares as well as defines (i.e, allocates memory to the variable x). No wonder why extern is used in declaring a variable that's gonna be used across various files.

Bahubali said:   9 years ago
What we declare before the main function it is called global declaration. Then we can use that variable any where in the program. If we declare the variable inside the main function it is called locally declaring the variable. We can use that variable only in specified function not out side the main function.

Sirajmuneer said:   1 decade ago
extern refers to suppose inside a local variable in main() we can access outside this variable its called extern and the same as fun() can be accessed
syntax:
main()
{
extern int s; //external declaration
}
fun1()
{
extern int s; //external declaration
}

int y; //definition


Post your comments here:

Your comments will be displayed after verification.