Variable scope in c

Scope of a variable in c programming languages specifies the accessibility (or use) of a variable in a program.

In c variables have following scope

  1. Local scope
  2. Global scope
  3. Formal parameters

Local Scope

Variables declared inside a function have local scope and are only accessible within the function. These variables cannot be accessed outside the function.

Global Scope

Variables declared outside of function have global scope and are accessible in the entire program.

Formal parameters

Variables declared in function as parameters, these parameters have more priority then global variables.

Example

#include<stdio.h>

int PI = 3.14; // Global Variable

void add(int, int);
void main()
{
	int amount = 200; // Local variable
	add(10,20);
}

void add(int no1, int no2) // Formal parameters
{
	printf("%d", no1 + no2);
}