I have problem to understand what is the function of 'extern' and how to use it..?..Hope you all will explain to me about this function and give some examples..
Your help and attention is much appreciated. Thank you so much..
test.c
#include<stdio.h>
externint var;
externint foo();
void main()
{
printf("value of var from foo: %d\n", foo());
printf("accessing var directly:%d\n ", var);
}
test1.c
int var; /*global to this file and can be extern-ed by another file*/
int foo(void)
{
return ++var;
}
Compiling:
gcc -o test test.c test1.c
Output:
~/test $ ./test
value of var from foo: 1
accessing var directly:1
In the case of extern"C", it specifies that the identifier does/will have C linkage. In other words, it is used to suppress C++ name mangling, which enables a C++ function to be called from C or, the other way around, a C function to be called in C++.