Global and Local Variables

A global variable’s scope is throughout the program in which the variable is declared, from any where, in any function we can modify the variable and local variables are limited to the block only in which they are defined declared declared, outside local block if local variables are accessed they produce error. Example

Example

#include <iostream>

int a = 20; // global variable
void function1() {
	std::cout << "\ninside function "<<a;
	// error here to access b, because b is local to main() function
	std::cout <<b; 
}

int main() {
	std::cout << a;
	int b = 200; // local variable to main function
	std::cout << b;	
	function1();
	getchar();
	return 0;
}