0% found this document useful (0 votes)
9 views

C_Structures_Unions_Exercises 2

The document contains exercises on C programming focusing on structures and unions. It includes implementations for basic structures, arrays of structures, nested structures, functions with structures, and union demonstrations. Each section provides code examples and expected outputs to illustrate the concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

C_Structures_Unions_Exercises 2

The document contains exercises on C programming focusing on structures and unions. It includes implementations for basic structures, arrays of structures, nested structures, functions with structures, and union demonstrations. Each section provides code examples and expected outputs to illustrate the concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

C Programming: Structures and Unions Exercises

1. Basic Structure Implementation


struct Student {
char name[50];
int roll;
float marks;
};

int main() {
struct Student s;
printf("Enter name, roll number, and marks: ");
scanf("%s %d %f", s.name, &s.roll, &s.marks);
printf("\nStudent Details:\nName: %s\nRoll No: %d\nMarks: %.2f\n", s.name, s.roll, s.marks);
return 0;
}
Output:
Enter name, roll number, and marks: John 101 85.5
Student Details:
Name: John
Roll No: 101
Marks: 85.50

2. Array of Structures
struct Employee {
int id;
char name[50];
float salary;
};

int main() {
struct Employee emp[5], max_emp;
int i;
printf("Enter details of 5 employees (id, name, salary):\n");
for (i = 0; i < 5; i++) {
scanf("%d %s %f", &emp[i].id, emp[i].name, &emp[i].salary);
if (i == 0 || emp[i].salary > max_emp.salary) max_emp = emp[i];
}
printf("\nEmployee with highest salary:\nID: %d\nName: %s\nSalary: %.2f\n", max_emp.id, max_emp.name,
max_emp.salary);
return 0;
}
3. Nested Structures
struct Address {
char city[50];
int ward_number;
};

struct Person {
char name[50];
int age;
struct Address address;
};

int main() {
struct Person p;
printf("Enter name, age, city, and ward number: ");
scanf("%s %d %s %d", p.name, &p.age, p.address.city, &p.address.ward_number);
printf("\nPerson Details:\nName: %s\nAge: %d\nCity: %s\nWard No: %d\n", p.name, p.age, p.address.city,
p.address.ward_number);
return 0;
}

4. Structure with Functions


struct Distance { int feet; float inches; };

struct Distance addDistance(struct Distance d1, struct Distance d2) {


struct Distance result;
result.feet = d1.feet + d2.feet;
result.inches = d1.inches + d2.inches;
if (result.inches >= 12.0) { result.feet += (int)(result.inches / 12); result.inches = (int)result.inches % 12; }
return result;
}

int main() {
struct Distance d1 = {5, 9.5}, d2 = {3, 4.5}, sum;
sum = addDistance(d1, d2);
printf("Sum of distances: %d feet %.1f inches\n", sum.feet, sum.inches);
return 0;
}

5. Union Demonstration
union Data { int i; float f; char ch; };

int main() {
union Data data;
data.i = 10;
printf("Integer: %d\n", data.i);
data.f = 20.5;
printf("Float: %.2f\n", data.f);
data.ch = 'A';
printf("Character: %c\n", data.ch);
printf("Integer after modifying char: %d\n", data.i);
return 0;
}

You might also like