Real-Life Example
Often in our examples, we simplify variable names to match their data type (myInt or myNum for int types, myChar for char types, and so on). This is done to avoid confusion..
However, if you want a real-life example on how variables can be used, take a look at the following, where we have made a program that stores different data of a college student:
Example
#include
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
Calculate the Area of a Rectangle:
Calculate Real-World Areas with a Rectangle Program (Length x Width):
Example
#include
int main() {
// Create integer variables
int length = 4;
int width = 6;
int area;
// Calculate the area of a rectangle
// OK, but not so easy to understand what m actually is
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