In the previous section, we discussed the basics of C Structures, Unions, and Enums. Now let us take a look at some real-life application Problems.
1. Display All Students Using Structure
Write a C program using a structure to store student details such as name, roll number, age and marks and display all students.
#include
struct Student {
char name[30];
int rollNo;
int age;
float marks;
};
int main() {
struct Student s[50];
int n, i;
printf("Enter number of students: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("\nEnter details of student %d\n", i + 1);
printf("Name: ");
scanf("%s", s[i].name);
printf("Roll Number: ");
scanf("%d", &s[i].rollNo);
printf("Age: ");
scanf("%d", &s[i].age);
printf("Marks: ");
scanf("%f", &s[i].marks);
}
printf("\nStudent Details \n");
for (i = 0; i < n; i++) {
printf("\nStudent %d\n", i + 1);
printf("Name : %s\n", s[i].name);
printf("Roll No : %d\n", s[i].rollNo);
printf("Age : %d\n", s[i].age);
printf("Marks : %.2f\n", s[i].marks);
}
return 0;
}
// Enter number of students: 3
// Enter details of student 1
// Name: Smith
// Roll Number: 23
// Age: 21
// Marks: 56
// Enter details of student 2
// Name: Amulya
// Roll Number: 56
// Age: 22
// Marks: 89
// Enter details of student 3
// Name: Cook
// Roll Number: 24
// Age: 21
// Marks: 62
// Student Details
// Student 1
// Name : Smith
// Roll No : 23
// Age : 21
// Marks : 56.00
// Student 2
// Name : Amulya
// Roll No : 56
// Age : 22
// Marks : 89.00
// Student 3
// Name : Cook
// Roll No : 24
// Age : 21
// Marks : 62.00
2. UNION – Sensor Data Storage
Write a C program using a union to store either temperature or pressure and display the stored value.
#include
union Sensor {
float temperature;
int pressure;
};
int main() {
union Sensor s;
int choice;
printf("1.Temperature 2.Pressure\n");
printf("Enter choice: ");
scanf("%d", &choice);
if (choice == 1) {
printf("Enter temperature: ");
scanf("%f", &s.temperature);
printf("Temperature = %.2f", s.temperature);
} else {
printf("Enter pressure: ");
scanf("%d", &s.pressure);
printf("Pressure = %d", s.pressure);
}
return 0;
}
// 1.Temperature 2.Pressure
// Enter choice: 1
// Enter temperature: 28
// Temperature = 28.00
3. UNION – Memory Demonstration
Demonstrate how a union shares memory.
#include
union Data {
int i;
float f;
char c;
};
int main() {
union Data d;
d.i = 10;
printf("i = %d\n", d.i);
d.f = 5.5;
printf("f = %.2f\n", d.f);
d.c = 'A';
printf("c = %c\n", d.c);
printf("Size of union = %lu", sizeof(d));
return 0;
}
// i = 10
// f = 5.50
// c = A
// Size of union = 4
4. Enum – Traffic System
Write a C program that uses an enum to represent Traffic Light states (RED, YELLOW, GREEN). Use a switch statement to print the action a driver should take for each color.
#include
enum TrafficLight {
RED, // 0
YELLOW, // 1
GREEN // 2
};
int main() {
enum TrafficLight currentSignal;
int input;
printf("Enter Signal (0 for RED, 1 for YELLOW, 2 for GREEN): ");
scanf("%d", &input);
currentSignal = (enum TrafficLight)input; // Typecasting integer to enum
switch (currentSignal) {
case RED:
printf("ACTION: STOP! The light is Red.\n");
break;
case YELLOW:
printf("ACTION: CAUTION! Prepare to stop or proceed carefully.\n");
break;
case GREEN:
printf("ACTION: GO! The path is clear.\n");
break;
default:
printf("Invalid Signal Code.\n");
}
return 0;
}
// Enter Signal (0 for RED, 1 for YELLOW, 2 for GREEN): 2
// ACTION: GO! The path is clear.
5. STRUCT + ENUM – Examination Result
Use a structure and an enum to store student marks and display PASS or FAIL.
#include
enum Result { FAIL, PASS };
struct Student {
int marks;
enum Result res;
};
int main() {
struct Student s;
printf("Enter marks: ");
scanf("%d", &s.marks);
if (s.marks >= 40)
s.res = PASS;
else
s.res = FAIL;
if (s.res == PASS)
printf("PASS");
else
printf("FAIL");
return 0;
}
// Enter marks: 58
// PASS
6. STRUCT + UNION + ENUM – ATM System
Design an ATM system using an enum for transaction type, a union for amount, and a structure for account. The account has a default balance of 5000.
#include
enum Type { DEPOSIT, WITHDRAW };
union Amount {
float money;
};
struct Account {
float balance;
enum Type type;
union Amount amt;
};
int main() {
struct Account a;
a.balance = 5000;
printf("0-Deposit 1-Withdraw: ");
scanf("%d", &a.type);
printf("Enter amount: ");
scanf("%f", &a.amt.money);
if (a.type == DEPOSIT)
a.balance += a.amt.money;
else
a.balance -= a.amt.money;
printf("Updated Balance = %.2f", a.balance);
return 0;
}
// 0-Deposit 1-Withdraw: 1
// Enter amount: 2200
// Updated Balance = 2800.00
// 0-Deposit 1-Withdraw: 0
// Enter amount: 450
// Updated Balance = 5450.00