Declaration→ Telling the compiler about the variable (No space reserved)
Definition→ Declaration + space reservation
//saved as harry.c
int main()
{
int harry = 90;
printf("%d",harry);
}
//saved as main.c
#include "harry.c"
extern int harry;
int main()
{
harry=56;
printf("%d",harry);
}
#include <stdio.h>
int myfunc(int a, int b)
{
// int sum=10; //this makes a local variable and it is used
extern int sum; // if we do this the compiler will know that
//this is a global variable either define globally or imported from other c file
return sum;
}
int sum = 343;
int main()
{
int sum=myfunc(3,5);
return 0;
}
#include <stdio.h>
int myfunc()
{
static int myVar;
myVar++; //incrementing each time the function is called
printf("myVar is %d\n",myVar);
}
int sum = 343;
int main()
{
myfunc(); //calling 1st time
myfunc(); //calling 2nd time
myfunc(); //calling 3rd time
myfunc(); //calling 4th time
return 0;
}
Output:
myVar is 1
myVar is 2
myVar is 3
myVar is 4
→ as the variable is declared as static which freeze the variable and previous
value is stored in it.