Global variables in C are declared outside any functions and usually at the top of the program as shown below. They can be used in the program inside any block of code.
Properties of a Global Variable:
- Global variables are defined outside a function, usually on top of the program.
- The lifetime of a Global Variable is throughout the program and can be accessed to any function or block.
Example: C Program for Local Variable
#include <stdio.h>
//Global Variable
int a = 20;
void funGlobal()
{
/*local variable with
same name on different block*/
int a = 50;
printf("%d : Inside funGlobal Function\n", a);
}
int main()
{
printf("%d : Inside main\n", a);
//caling the function
funGlobal();
}
The Output of Global Variable in C.
20 : Inside main
50 : Inside funGlobal Function