[go: up one dir, main page]

0% found this document useful (0 votes)
8 views5 pages

C_Structures_Unions_Exercises_Full

The document contains a series of C programming exercises focused on structures and unions. It includes implementations of basic structures, arrays of structures, nested structures, and unions, as well as examples demonstrating structure pointers and typedefs. Additionally, it highlights the differences between structures and unions and provides combined examples of both.
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)
8 views5 pages

C_Structures_Unions_Exercises_Full

The document contains a series of C programming exercises focused on structures and unions. It includes implementations of basic structures, arrays of structures, nested structures, and unions, as well as examples demonstrating structure pointers and typedefs. Additionally, it highlights the differences between structures and unions and provides combined examples of both.
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/ 5

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;
}

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;
}

6. Structure Pointers
struct Book {
char title[50];
char author[50];
float price;
};

int main() {
struct Book b = {"C Programming", "Dennis Ritchie", 299.50};
struct Book *ptr = &b;

ptr->price = 350.75;

printf("Updated Book Details:\nTitle: %s\nAuthor: %s\nPrice: %.2f\n", ptr->title, ptr->author, ptr->price);


return 0;
}

7. Typedef with Structures


typedef struct {
int book_id;
char title[50];
int pages;
} BookRecord;

int main() {
BookRecord book;
printf("Enter book ID, title, and pages: ");
scanf("%d %s %d", &book.book_id, book.title, &book.pages);

printf("\nBook Record:\nID: %d\nTitle: %s\nPages: %d\n", book.book_id, book.title, book.pages);


return 0;
}

8. Union for Mixed Data Types


union Value {
int i;
float f;
char str[20];
};

int main() {
union Value val;

val.i = 10;
printf("Integer: %d\n", val.i);

val.f = 25.75;
printf("Float: %.2f\n", val.f);

strcpy(val.str, "Hello");
printf("String: %s\n", val.str);

return 0;
}

9. Difference Between Struct and Union


struct StructExample {
int x;
float y;
};

int main() {
struct StructExample s;
printf("Size of Structure: %lu\n", sizeof(s));
return 0;
}

union UnionExample {
int x;
float y;
};

int main() {
union UnionExample u;
printf("Size of Union: %lu\n", sizeof(u));
return 0;
}

10. Combined Struct and Union


struct Employee {
int id;
char name[50];
union JobType {
int contract_duration;
float salary;
} job;
int isPermanent;
};

int main() {
struct Employee e1 = {101, "John", .job.contract_duration = 12, 0};
struct Employee e2 = {102, "Alice", .job.salary = 60000.50, 1};

printf("Contract Employee:\nID: %d\nName: %s\nContract Duration: %d months\n", e1.id, e1.name,


e1.job.contract_duration);
printf("\nPermanent Employee:\nID: %d\nName: %s\nSalary: %.2f\n", e2.id, e2.name, e2.job.salary);

return 0;
}

You might also like