In this tutorial you will learn about the C Scope Rules and its application with practical example.
Scope defines the region of visibility or availability for a variable, out of which we can not reference that variable. There are two main kinds of scope.
Global variables can be accessed at any point throughout the program, and can be used in any function. There is single copy of the global variable is available.Global variables are defined outside of all the functions, usually on top of the program.
Variable declared inside a function or block of code are called local variables. They can be accessed only inside that function or block of code. Local variables are not available to outside function.
/* global variable declaration */
int main ()
/* local variable declaration */
int a, b;
/* actual initialization */
a = 10;
b = 20;
sum = a + b;
printf ("value of a = %d, b = %d and sum of (a and b) is = %d\n", a, b, sum);
Note :- A program can have same name for local and global variables but local variable overrides the value.