C Programming - Declarations and Initializations - Discussion

4. 

Is there any difference between following declarations?
1 : extern int fun();
2 : int fun();

[A]. Both are identical
[B]. No difference, except extern int fun(); is probably in another file
[C]. int fun(); is overrided with extern int fun();
[D]. None of these

Answer: Option B

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.


Shobhit said: (Aug 17, 2010)  
I don't understand the extern. please tell me what is extern.

Pradeep Bhupathi said: (Aug 25, 2010)  
Extern is a keyword which is used to define any variable or function in externally.

Ashwini said: (Sep 25, 2010)  
What is global function?

Nagarjuna said: (Oct 3, 2010)  
How can we declare functions with extern keyword and how can we access that function ?

Rishikumar said: (Oct 13, 2010)  
How can we declare functions with extern keyword and how can we access that function ?

Ganesh said: (Jan 8, 2011)  
Extern keyword not understand, please explain.

Smsirajmuneer said: (Jan 17, 2011)  
'extern' is similar to the gloabal declaration we can access any where in the program module, similar to accessing the functions in .dll files from various .exe files.

Sirajmuneer said: (Jan 17, 2011)  
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

Pankaj said: (Feb 5, 2011)  
The extern int fun(); is nothing but it can be declared globlly means out side the function.

But int fun(); can be declared in local means in the function only.

Nancy said: (Mar 5, 2011)  
While using extern keyword, memory is allocated to variable or not?

Wikiok said: (Mar 10, 2011)  
@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."

Ganesh said: (Mar 15, 2011)  
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: (Jul 7, 2011)  
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: (Sep 7, 2011)  
Then 'extern' keyword not understand, please explain.

Sanket V Garg said: (Sep 22, 2011)  
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: (Oct 17, 2011)  
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: (Jan 7, 2012)  
I didn't got suitable answer for "extern" keyword can any one explain with example? please ?

Khusi said: (Feb 29, 2012)  
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: (Mar 1, 2012)  
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: (May 31, 2012)  
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.

Coder said: (Jun 12, 2012)  
All functions are defined as extern in default.
If we declare a function as, int foo() actually it is taken as
extern int foo.

Chand890 said: (Oct 23, 2012)  
What is extern?

Sm Das said: (Oct 27, 2012)  
It is a keyword which functioning same as global and local variables.

Vijay said: (Jun 27, 2013)  
Why we use extern function in C?

Subhasish said: (Jul 3, 2013)  
Could anybody please tell me what is the scope for extern, I mean; say we have two files in two different directories I want to use one variable/function of file1 in file2 but those 2 are in diff. Directories. So can I access the elements of file1 in file2 using extern?

Pradeep said: (Jul 4, 2013)  
Does storage classes can be used with data types and keywords.

Preethe said: (Jul 9, 2013)  
What is meant by global variables? please explain.

Mumukshu said: (Jul 29, 2013)  
The answer is incorrect. In C language, by default all function declarations are extern as C does not support local functions. Extern func(); and func(); are the same.

Ragaveni said: (Aug 17, 2013)  
Variables which are declared before main function i.e. not in any function are called global variables.

Radheshyam said: (Dec 27, 2013)  
"Extern" is keyword in C ,suppose you write 2 diff. function in diff. file and main function in third file,

And you want one variable use globally eg. int a;

At that time we use extern keyword eg. extern int a;

This procedure same for function.

Eshwar said: (Feb 23, 2014)  
Hi,

How is extern function different from normal function?

If a function is declared as extern, then that function can be overrided?

Sirisha said: (Jun 27, 2014)  
What is global function and why extern keyword used to declare a function?

Vijay Sawant said: (Jun 30, 2014)  
extern int a;

extern keyword tell the compiler, identifier a of type integer define anywhere through the program. This statement is just declaration.

Vijay said: (Jun 30, 2014)  
@Ragveni.

Global variable means that the variable which are defined outside all the blocks of functions, anywhere in file (not only before main).

main()
{
pf("hello");
}
int a;

a is global variable.

Shyojee Ram Meena said: (Jul 14, 2014)  
Declaration means that the variable exist in program :-

#include<stdio.h>
main()
{
int a;
printf("%d",&a);
return 0;
}
Why it gives address of memory location?

Arvind Kumar said: (Aug 29, 2014)  
It gives the address of memory location so we can use the specific value.

Arzoo said: (Sep 16, 2014)  
Please explain function using extern keyword with proper example.

Adithya said: (Jan 13, 2015)  
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.

Raj said: (Apr 12, 2015)  
Can anyone tell me how can we access one C program to another one with external variable.

Revathi said: (Sep 29, 2015)  
What is the different between normal function and extern function?

Priyanka Chavan said: (Dec 26, 2015)  
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.

Anum said: (May 7, 2016)  
Extern is a keyword, which informs the compiler that the function or variable has external linkage.

The syntax is:

extern string-literal { declaration-list }
extern string-literal declaration

Bhyresh said: (May 18, 2016)  
What is the difference between int main and extern int main?

Bahubali said: (Jun 19, 2016)  
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.

Dulal said: (Jul 8, 2016)  
What is the global function?

Sruthi said: (Aug 10, 2016)  
Is the variable declared as extern similar to the variable declared under main?

Harvansh Singh said: (Oct 6, 2016)  
What is the actual meaning of extern keyword? Explain clearly.

Keerthi said: (Mar 18, 2017)  
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" )

Jhansi said: (Jun 17, 2017)  
What is extern? I can't get it.

Harish said: (Jul 6, 2017)  
Extern is a keyword to storage class external.

Whose memory allocation will be done in a data area.
SCOPE of the variable with extern declaration is global. (It's reduce code security levels )

Thanks for your example @Keerthi.

Ranjith said: (Sep 12, 2017)  
What the extern function to do?

But how could you say that there is no difference between the two? Please tell me.

Venkat said: (Jan 3, 2018)  
@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.

Suraj said: (Apr 24, 2018)  
Thanks for explaining the answer.

Neha said: (Jul 30, 2018)  
I am not understanding this, please explain it for me.

Bhargavi said: (Sep 26, 2018)  
Please tell the difference between extern and static, automatic.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.