Tuesday 22 November 2016

extern keyword: This discussion will be very useful for beginners with C language.


Before going for extern we look at the C variable declaration and definition

Declaration:  Declaration of a variable simply states that the variable exists somewhere in the program in that scope. While memory is not allocated to the variable.

Definition: Definition is the super-set of declaration. Variable definition means that a variable is declared and memory is allocated to that variable.

extern keyword extends the visibility of the C variables and functions throughout the program.
i.e u can define a variable once and use any where in the program with the help of extern.

lets understand with example program

filename: new.c
extern int ll;
void fun()
{
printf("hello=%d\n",ll);
return;
}


filename: prog.c
#include<stdio.h>
#include"new.c"
int ll=10;
void fun();
int main()
{
fun();
return 0;
}
in new.c  we are just referring to variable ll and that can present any where in the program, ll is defined and declared in prog.c
when u run the program get 10 as output.

Now we can call extern int ll as declaration and int ll=10 as definition.

Happy Coding!!!!

1 comment: