Real-Life Examples of Variables in C
- Why Use Variables in Real Life?
In real programs, we use variables to store and manage data — such as a student’s details, product price, employee salary, etc.
Variables make it easy to update, calculate, and display data whenever needed.
🧩 Example 1: Student Data Program
Let’s create a program that stores information about a student and prints it.
Code:
#include <stdio.h>
int main() {
// Student data
int studentID = 15;
int studentAge = 23;
float studentFee = 75.25;
char studentGrade = ‘B’;
// Print variables
printf(“Student ID: %d\n”, studentID);
printf(“Student Age: %d\n”, studentAge);
printf(“Student Fee: %f\n”, studentFee);
printf(“Student Grade: %c”, studentGrade);
return 0;
}
✅ Output:
Student ID: 15
Student Age: 23
Student Fee: 75.250000
Student Grade: B
🔹 2. Explanation
|
Variable |
Type |
Description |
Example Value |
|
studentID |
int |
Unique number to identify the student |
15 |
|
studentAge |
int |
Student’s age |
23 |
|
studentFee |
float |
Student’s course fee (decimal value) |
75.25 |
|
studentGrade |
char |
Grade in a subject |
‘B’ |
🧩 Example 2: Calculate the Area of a Rectangle
Let’s create a program that calculates the area using length × width.
Code:
#include <stdio.h>
int main() {
// Create integer variables
int length = 4;
int width = 6;
int area;
// Calculate the area of a rectangle
area = length * width;
// Print the variables
printf(“Length is: %d\n”, length);
printf(“Width is: %d\n”, width);
printf(“Area of the rectangle is: %d”, area);
return 0;
}
✅ Output:
Length is: 4
Width is: 6
Area of the rectangle is: 24
- Explanation
|
Variable |
Description |
Example |
|
length |
Rectangle’s length |
4 |
|
width |
Rectangle’s width |
6 |
|
area |
Result of length × width |
24 |
📘 Formula Used:
[
\text{Area} = \text{Length} \times \text{Width}
]
Quick Summary
|
Concept |
Example |
Description |
|
Store details |
Student info program |
Real-life data storage |
|
Perform calculations |
Rectangle area |
Use math operations |
|
Print output |
printf() |
Display result |