What is the difference between local and global variables in C?

Local variableGlobal variable
Declared inside a function or blockDeclared outside all functions
Can be used only in that blockCan be used in all functions
Default value is garbageDefault value is 0
Removed when the function endsLives until the program ends

Try to use local variables as much as possible. Global variables can be changed from anywhere, so bugs are hard to find. See Scope and Storage Classes.


Learn more about the similar topics:
Tutorials
C Functions
C Function Arguments and Return Value
C Call by Value and Call by Reference
C Recursion
C Scope and Storage Classes
Exercises & Assignments
Write a C program to find the factorial of a number using recursion
Write a C program to swap two numbers using a function and pointers
Interview Questions & Answers
What is the difference between call by value and call by reference in C?
What is recursion in C?
What is the difference between local and global variables in C?
What are storage classes in C?
What is the use of the static keyword in C?