Variables in C
A variable in C is a named memory location used to store data.
Its value can change during the execution of a program, which is why the name βvariableβ fits perfectly.
Understanding variables is essential because every program works with data.
What is a Variable?
A variable is a name given to a memory location that stores a value. The value can be updated anytime in the program.
int age = 20; float marks = 89.5; char grade = 'A';
Here, age, marks, and grade are variables.
How Variables Work in Memory?
Variable Name β age Data Type β int Value β 20 Memory Example β stored at address 0xA5C7
Variables are just names pointing to memory blocks.
Rules for Naming Variables
- Must start with a letter or underscore (_)
- Cannot start with a number
- Can contain letters, digits, underscores
- No spaces allowed
- Case-sensitive (Total β total)
- Cannot use keywords (int, float, return, etc.)
- Should be meaningful
β Valid:
height, _value, student1
β Invalid: 1age, total marks, char
Types of Variables
C supports several types of variables based on scope and storage:
- Local variables β only inside a function/block
- Global variables β declared outside all functions
- Static variables β preserve value between function calls
- Automatic variables β default inside functions
- External variables β defined outside but used with extern
Examples of Variable Types
1. Local Variable
void test() {
int x = 10; // local variable
printf("%d", x);
}
Accessible only inside test().
2. Global Variable
int count = 5; // global variable
void show() {
printf("%d", count);
}
Accessible in any function inside the file.
3. Static Variable
void example() {
static int num = 0;
num++;
printf("%d ", num);
}
Even after the function ends, num will preserve its value.
4. Extern Variable
extern int score; // variable exists somewhere else
Used when sharing data across files.
Variable Declaration vs Definition
Declaration β Telling compiler the variable exists Definition β Allocating memory for that variable
Example:
extern int a; // declaration int a = 10; // definition
Variable Initialization
Assigning a value at the time of declaration.
int a = 10; float price = 50.75; char grade = 'A';
Multiple Variable Declaration
int x, y, z; float a = 2.5, b = 3.4;
Memory Size of Variables
Size depends on data type:
int β 4 bytes float β 4 bytes double β 8 bytes char β 1 byte
Practical Example
#includeint main() { int age = 21; float percentage = 89.50; char grade = 'A'; printf("Age: %d\n", age); printf("Percentage: %.2f\n", percentage); printf("Grade: %c\n", grade); return 0; }
Practical Example 2 β Updating Values
#includeint main() { int count = 5; float price = 99.99; printf("Initial Count: %d\n", count); printf("Initial Price: %.2f\n", price); // Updating values count = 10; price = 149.50; printf("Updated Count: %d\n", count); printf("Updated Price: %.2f\n", price); return 0; }
Practical Example 3 β Mixed Data Types
#includeint main() { int roll = 101; char section = 'B'; float height = 5.9; printf("Roll Number: %d\n", roll); printf("Section: %c\n", section); printf("Height: %.1f feet\n", height); return 0; }
Practical Example 4 β Local & Global Variables
#includeint total = 50; // Global variable int main() { int total = 10; // Local variable (shadows global) printf("Local total: %d\n", total); // C doesn't support :: to access global when shadowed. // Demonstration with another name: printf("Global total: %d\n", ::total); // Not valid in C, example for concept only return 0; }
Note: C does NOT support :: like C++.
This example is only to explain local vs global variable concept.
Practical Example 5 β Static Variables
#includevoid counter() { static int num = 1; // static variable printf("Count: %d\n", num); num++; // retains its value } int main() { counter(); counter(); counter(); return 0; }
Output:
1
2
3
Practice Questions
- What is a variable? Explain with examples.
- Write 10 valid and 10 invalid identifiers.
- Differentiate between local and global variables.
- What is the difference between declaration and definition?
- Explain static variables with an example.
Practice Task
Write a C program that uses:
- 3 local variables
- 1 global variable
- 1 static variable