Thursday, August 28, 2014

extern variable and fucntions

self explanatory:

without extern:


root@selvakumar-Lenovo-B460e:/C_pgms/extern-var# cat one.c two.c

int a=10;

#include<stdio.h>
int main()
{
//extern int a;
a = 20;
printf("\n%d\n",a);
return 0;
}
root@selvakumar-Lenovo-B460e:/C_pgms/extern-var# gcc one.c two.c
two.c: In function ‘main’:
two.c:5:2: error: ‘a’ undeclared (first use in this function)
two.c:5:2: note: each undeclared identifier is reported only once for each function it appears in
root@selvakumar-Lenovo-B460e:/C_pgms/extern-var#




with extern

root@selvakumar-Lenovo-B460e:/C_pgms/extern-var# cat one.c two.c

int a=10;

#include<stdio.h>
int main()
{
extern int a;
a = 20;
printf("\n%d\n",a);
return 0;
}
root@selvakumar-Lenovo-B460e:/C_pgms/extern-var# gcc one.c two.c
root@selvakumar-Lenovo-B460e:/C_pgms/extern-var# ./a.out

20
root@selvakumar-Lenovo-B460e:/C_pgms/extern-var#



Another example:

points need to be caputred:

  • in three.c file a is global variable and when compiled then a will be global in the executable and a in the four.c is the local variable a and so no effect happened


root@selvakumar-Lenovo-B460e:/C_pgms/extern-var# cat three.c four.c
int a=10;
void fun()
{
       printf("\n%d\n",a);
}

#include<stdio.h>
int main()
{

int a = 20;
printf("\n%d\n",a);
fun();
return 0;
}
root@selvakumar-Lenovo-B460e:/C_pgms/extern-var# gcc three.c four.c
three.c: In function ‘fun’:
three.c:4:1: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
root@selvakumar-Lenovo-B460e:/C_pgms/extern-var# ./a.out

20

10
root@selvakumar-Lenovo-B460e:/C_pgms/extern-var#


Another example:
----------------------
points need to be caputred:

  • in three.c file a is global variable and when compiled then a will be global in the executable and a in the four.c is also the global variable a and so problem arises.
  • this problem can avoided in two ways 
  • way 1: can declare a in the three.c as static
  • way 2: instead of declaring the variable in four.c we can make a in four.c as extern

root@selvakumar-Lenovo-B460e:/C_pgms/extern-var# cat three.c four.c 
int a=10;
void fun()
{
printf("\n%d\n",a);
}

#include<stdio.h>
int a = 20;
int main()
{
printf("\n%d\n",a);
fun();
return 0;
}
root@selvakumar-Lenovo-B460e:/C_pgms/extern-var# gcc three.c four.c 
three.c: In function ‘fun’:
three.c:4:1: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
/tmp/cc8PMgA6.o:(.data+0x0): multiple definition of `a'
/tmp/ccbqbc2L.o:(.data+0x0): first defined here
collect2: ld returned 1 exit status
root@selvakumar-Lenovo-B460e:/C_pgms/extern-var# 


way1:

Points need to be captured:
  • so i made variable a in the file three.c as static and this is very same rule for the function declaration also
root@selvakumar-Lenovo-B460e:/C_pgms/extern-var/way1# cat three.c four.c 
static int a=10;
void fun()
{
printf("\n%d\n",a);
}

#include<stdio.h>
int a = 20;
int main()
{
printf("\n%d\n",a);
fun();
return 0;
}
root@selvakumar-Lenovo-B460e:/C_pgms/extern-var/way1# gcc three.c four.c 
three.c: In function ‘fun’:
three.c:4:1: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
root@selvakumar-Lenovo-B460e:/C_pgms/extern-var/way1# ./a.out 

20

10
root@selvakumar-Lenovo-B460e:/C_pgms/extern-var/way1# 

way2:

root@selvakumar-Lenovo-B460e:/C_pgms/extern-var/way2# cat three.c four.c 
int a=10;
void fun()
{
printf("\n%d\n",a);
}

#include<stdio.h>
extern int a; // observe that when i am using extern then no declaration happen as next  line
// because declaration should be in any one source file and linking
// using any number of file 
//int a = 20;
int main()
{
printf("\n%d\n",a);
fun();
return 0;
}
root@selvakumar-Lenovo-B460e:/C_pgms/extern-var/way2# gcc three.c four.c 
three.c: In function ‘fun’:
three.c:4:1: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
root@selvakumar-Lenovo-B460e:/C_pgms/extern-var/way2# ./a.out 

10

10
root@selvakumar-Lenovo-B460e:/C_pgms/extern-var/way2# 



extern function:


when we write.
    int foo(int arg1, char arg2);
There’s an extern present in the beginning which is hidden and the compiler treats it as below.
    extern int foo(int arg1, char arg2);

* Therefore whenever we define a C function, an extern is present there in the beginning of the function definition. Since the declaration can be done any number of times and definition can be done only once, we can notice that declaration of a function can be added in several C/H files or in a single C/H file several times. 

* But we notice the actual definition of the function only once (i.e. in one file only). And as the extern extends the visibility to the whole program, the functions can be used (called) anywhere in any of the files of the whole program provided the declaration of the function is known. (By knowing the declaration of the function, C compiler knows that the definition of the function exists and it goes ahead to compile the program). 

No comments:

Post a Comment