In this article “Types of Variables in C”, we explain how variables are categorized based on their scope and lifetime.
Types of Variable in C:
The scope of a variable defines where the variable can be accessed or used within a program. A variable can only be used inside the block or function in which it is declared, unless it is a global variable.
Introduction
A variable is a name given to a memory location that stores data or information. In the C language, every variable must be declared before it is used. Depending on where the variable is declared, it can either be local or global.
Types of Variables in C
C language supports two main types of variables:
- Local Variables
- Global Variables
The classification depends on where the variable is declared in the program.
-
Local Variables in C
A local variable is declared inside a function or block and can only be used within that specific function or block. Once the function execution ends, the local variable is destroyed and its memory is released.
Example:
#include <stdio.h>
void main ()
{
int p, q; // local variable declaration
int sum;
p = 15; // initialization
q = 25;
sum = p + q;
printf(“%d + %d = %d\n”, p, q, sum);
}
Output:
15 + 25 = 40
In this example, the variables p, q, and sum are local variables because they are declared inside the main() function and cannot be accessed outside it.
Advantages of Local Variables
- Each function can have its own set of variables with the same names without conflict.
- Memory for local variables is allocated only during function execution and released afterward, making memory usage efficient.
- Local variables reduce the chances of accidental modification of values by other functions.
Disadvantages of Local Variables
- The variable is not accessible outside the function in which it is declared.
- Data sharing between functions is not possible.
- Debugging can become difficult in large programs.
2. Global Variables in C
A global variable is declared outside all functions, generally between the header file declarations and the main() function. Such variables can be used by any function in the program.
Example:
#include <stdio.h>
int sum; // global variable declaration
void main ()
{
int p, q; // local variables
p = 10;
q = 20;
sum = p + q;
printf(“Value of p = %d, q = %d, and sum = %d\n”, p, q, sum);
}
Output:
Value of p = 10, q = 20, and sum = 30
Here, the variable sum is global, as it is declared outside all functions and can be accessed by any function in the program.
Local vs Global Variable Priority
If a local and a global variable have the same name, the local variable takes priority inside its function.
Example:
#include <stdio.h>
int sum = 20; // global variable declaration
void main ()
{
int sum = 10; // local variable declaration
printf(“Value of sum = %d\n”, sum);
}
Output:
Value of sum = 10
In this example, even though both variables are named sum, the local variable is used because it has higher priority within the main() function.
Advantages of Global Variables
- Global variables can be accessed by all functions in a program.
- They are declared only once and remain available throughout the program.
- Useful when multiple functions need to share common data.
Disadvantages of Global Variables
- Any function can change the value of a global variable, which may cause unexpected results.
- Using too many global variables increases the chances of program errors.
- Global variables occupy memory for the entire program duration, which may lead to memory wastage.
Formal Parameters
When a variable is declared inside the parentheses of a function definition, it is known as a formal parameter. Its scope is local to that function.
Example Program – Local and Global Variables
#include <stdio.h>
#include <conio.h>
int n; // global variable declaration
void square(); // function declaration void sum(); // function declaration
void main()
{
int n = 50; // local variable
clrscr();
printf(“\nValue of N: %d”, n);
square(); // function call
sum(); // function call
getch();
}
void square()
{
n = 7; // global variable
printf(“\nSquare of N: %d”, n * n);
}
void sum()
{
int m = 5; // local variable
n = 6; // global variable
printf(“\nSum of Two numbers: %d”, n + m);
}
Output:
Value of N: 50
Square of N: 49
Sum of Two numbers: 11
Conclusion
Variables in C can be local or global depending on where they are declared.
Local variables are confined to a function, while global variables can be accessed throughout the program.
Choosing between them depends on the scope and purpose of your data.
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining