In C programming, variables are one of the most fundamental concepts. A variable is a named memory location used to store data that can change during program execution.
Without variables, a program would have no way to store inputs, outputs, or temporary values needed for computations.
What Is a Variable?
A variable is a container (memory location) that holds a value. The value stored in a variable can be read, modified, or updated throughout the program.
Example:
int age = 20;
age = 25; // Value updated
Here, age is a variable storing the number 20, and later its value changes to 25.
Characteristics of Variables
- They are named locations in memory
- Their value can change
- They must be declared before use
- Their data type decides how much memory they occupy and what type of value they can store
Variable Declaration in C
Before using a variable, you must declare it.
Syntax:
data_type variable_name;
Examples:
int marks;
float price;
char grade;
Variable Initialization
Initialization means assigning an initial value at the time of declaration.
Example:
int age = 18;
float pi = 3.14;
char gender = 'M';
You can also declare multiple variables at once:
int a = 10, b = 20, c = 30;
Rules for Naming Variables
These rules are the same as identifiers:
- Must start with a letter or underscore
- Cannot start with a digit
- Cannot contain spaces
- Cannot use special characters (@, #, %, etc.)
- Cannot use keywords
- Case-sensitive
Valid variable names:
total
_sum
marks1
value2025
Invalid:
1value // starts with digit
student name // space not allowed
float*data // special character
return // keyword
Types of Variables in C
C categorizes variables based on their scope, lifetime, and storage class.
Below are the major types:
1. Local Variables
Declared inside a function or block, accessible only within that block.
void main() {
int x = 10; // local variable
}
They are destroyed once the function ends.
2. Global Variables
Declared outside all functions.
Accessible throughout the entire program.
int count = 0; // global variable
void display() {
printf("%d", count);
}
3. Static Variables
Retain their value even after the function ends.
void show() {
static int x = 0;
x++;
printf("%d ", x);
}
Calling show() multiple times prints:1 2 3 4 ...
4. Automatic Variables (auto)
These are local variables created automatically.
By default, all local variables are auto.
void main() {
auto int a = 10;
}
Using auto explicitly is rare.
5. External Variables (extern)
Used to declare a global variable in another file.
extern int total; // variable exists in another file
Memory Allocation Based on Variable Type
Different variable types occupy different memory sizes:
| Data Type | Typical Size |
|---|---|
char | 1 byte |
int | 2 or 4 bytes |
float | 4 bytes |
double | 8 bytes |
Memory size may vary depending on compiler and architecture.
Example Program Showing All Variable Types
#include <stdio.h>
int globalVar = 50; // global variable
void test() {
static int s = 0; // static variable
s++;
printf("Static: %d\n", s);
}
int main() {
int localVar = 10; // local variable
float price = 99.5; // initialized variable
printf("Local Variable: %d\n", localVar);
printf("Global Variable: %d\n", globalVar);
printf("Price: %.2f\n", price);
test();
test();
test(); // Observe static variable behavior
return 0;
}
Difference Between Variables and Constants
| Feature | Variable | Constant |
|---|---|---|
| Value | Changeable | Fixed |
| Defined by | Programmer | const or #define |
| Memory | Yes | Usually no (for #define) |
| Example | int age=20 | const int AGE=20 |
Conclusion
Variables play a crucial role in programming by enabling storage and manipulation of data. Understanding how to declare, initialize, modify, and categorize variables will help you write clean, efficient, and logically structured C programs.
🔗 View other articles about C Programming:
https://savanka.com/category/learn/c-programming