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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #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.
1 2 | 20 : Inside main 50 : Inside funGlobal Function |