variables that are known throughout the program and may be used by any piece of code is called a global variable. Also, they will hold their value throughout the program’s execution. Global variables can be created by declaring them outside of any function.
Global variables are helpful when many functions in the program use the same data. Storage for global variables is in a fixed region of memory set aside for this purpose by the compiler.
In the following program, the variable count has been declared outside of all functions.
Example:
#include "stdio.h" int count; /* count is global */ void func1(void); int main(void) { count = 100; func1(); return 0; } void func1(void) { printf("count is % d", count); /* will print 100 */ } |