C Programming • Structure and Union
C Programming / Functions and Structures — Exercises

Functions and Structures — Exercises

Practical 4 Structure and Union

Write a C program to create a structure for a student and pass the structure to a function to display the student's details.

Practical / Solution

Functions and Structures — Exercise 1

Problem

Write a C program to create a structure for a student and pass the structure to a function to display the student's details.

Program

#include <stdio.h> struct Student { int roll; char name[50]; float marks; }; void display(struct Student s) { printf("Roll = %d\n", s.roll); printf("Name = %s\n", s.name); printf("Marks = %.2f\n", s.marks); } int main() { struct Student s; printf("Enter roll number, name and marks: "); scanf("%d %49s %f", &s.roll, s.name, &s.marks); // Pass structure to function display(s); return 0; }

Explanation

A structure variable can be passed to a function as an argument. Here, the complete student structure is passed to the display() function by value.

Expected Output

Enter roll number, name and marks: 101 Rahul 85

Roll = 101

Name = Rahul

Marks = 85.00

Functions and Structures — Exercise 2

Problem

Write a C program to pass an employee structure to a function and calculate the employee's annual salary.

Program

#include <stdio.h> struct Employee { int id; char name[50]; float monthlySalary; }; float annualSalary(struct Employee e) { return e.monthlySalary * 12; } int main() { struct Employee e; float annual; printf("Enter employee ID, name and monthly salary: "); scanf("%d %49s %f", &e.id, e.name, &e.monthlySalary); annual = annualSalary(e); printf("\nEmployee = %s\n", e.name); printf("Annual Salary = %.2f\n", annual); return 0; }

Explanation

A structure can be passed to a function that performs a calculation using its members. Here, the function receives the employee record and returns the annual salary.

Expected Output

Enter employee ID, name and monthly salary: 501 Amit 45000

Employee = Amit

Annual Salary = 540000.00

Functions and Structures — Exercise 3

Problem

Write a C program in which a function receives a student structure and returns the total marks obtained in three subjects.

Program

#include <stdio.h> struct Student { int roll; char name[50]; int math; int cProgramming; int computer; }; int totalMarks(struct Student s) { return s.math + s.cProgramming + s.computer; } int main() { struct Student s; int total; printf("Enter roll number and name: "); scanf("%d %49s", &s.roll, s.name); printf("Enter marks in three subjects: "); scanf("%d %d %d", &s.math, &s.cProgramming, &s.computer); total = totalMarks(s); printf("\nStudent = %s\n", s.name); printf("Total Marks = %d\n", total); return 0; }

Explanation

The complete structure is passed to totalMarks(). The function accesses the subject members and calculates the total.

Expected Output

Enter roll number and name: 101 Priya

Enter marks in three subjects: 85 90 88

Student = Priya

Total Marks = 263

Functions and Structures — Exercise 4

Problem

Write a C program to pass a rectangle structure to a function and calculate its area.

Program

#include <stdio.h> struct Rectangle { float length; float width; }; float calculateArea(struct Rectangle r) { return r.length * r.width; } int main() { struct Rectangle r; float area; printf("Enter length and width: "); scanf("%f %f", &r.length, &r.width); area = calculateArea(r); printf("Area = %.2f\n", area); return 0; }

Explanation

The rectangle structure contains the dimensions of a rectangle. It is passed to a function which calculates and returns the area.

Expected Output

Enter length and width: 10 5

Area = 50.00

Functions and Structures — Exercise 5

Problem

Write a C program to use a function with a pointer to structure and increase an employee's salary by 10%.

Program

#include <stdio.h> struct Employee { int id; char name[50]; float salary; }; void increaseSalary(struct Employee *e) { e->salary = e->salary + (e->salary * 0.10); } int main() { struct Employee e; printf("Enter employee ID, name and salary: "); scanf("%d %49s %f", &e.id, e.name, &e.salary); increaseSalary(&e); printf("\nName = %s\n", e.name); printf("Updated Salary = %.2f\n", e.salary); return 0; }

Explanation

A pointer to a structure is passed to the function. The arrow operator -> is used to access structure members through the pointer. Changes made inside the function affect the original structure.

Expected Output

Enter employee ID, name and salary: 501 Amit 40000

Name = Amit

Updated Salary = 44000.00

Functions and Structures — Exercise 6

Problem

Write a C program in which a function returns a structure containing the details of a student.

Program

#include <stdio.h> struct Student { int roll; char name[50]; float marks; }; struct Student getStudent() { struct Student s; printf("Enter roll number: "); scanf("%d", &s.roll); printf("Enter name: "); scanf("%49s", s.name); printf("Enter marks: "); scanf("%f", &s.marks); return s; } int main() { struct Student s; s = getStudent(); printf("\nStudent Details\n"); printf("Roll = %d\n", s.roll); printf("Name = %s\n", s.name); printf("Marks = %.2f\n", s.marks); return 0; }

Explanation

A function can return a complete structure variable. In this example, getStudent() reads the student record and returns it to main().

Expected Output

Enter roll number: 102

Enter name: Neha

Enter marks: 92

Student Details

Roll = 102

Name = Neha

Marks = 92.00

Functions and Structures — Exercise 7

Problem

Write a C program to compare the marks of two students using a function that accepts two structure variables.

Program

#include <stdio.h> struct Student { int roll; char name[50]; float marks; }; struct Student compareStudents( struct Student s1, struct Student s2) { if (s1.marks > s2.marks) return s1; else return s2; } int main() { struct Student s1, s2, topper; printf("Enter first student's roll, name and marks: "); scanf("%d %49s %f", &s1.roll, s1.name, &s1.marks); printf("Enter second student's roll, name and marks: "); scanf("%d %49s %f", &s2.roll, s2.name, &s2.marks); topper = compareStudents(s1, s2); printf("\nStudent with higher marks:\n"); printf("Roll = %d\n", topper.roll); printf("Name = %s\n", topper.name); printf("Marks = %.2f\n", topper.marks); return 0; }

Explanation

Multiple structure variables can be passed to the same function. The function compares their members and returns the structure having higher marks.

Expected Output

Enter first student's roll, name and marks: 101 Rahul 78

Enter second student's roll, name and marks: 102 Priya 91

Student with higher marks:

Roll = 102

Name = Priya

Marks = 91.00

Functions and Structures — Exercise 8

Problem

Write a C program to modify the values of a rectangle structure by passing its address to a function.

Program

#include <stdio.h> struct Rectangle { float length; float width; }; void modifyRectangle(struct Rectangle *r) { r->length = r->length + 2; r->width = r->width + 3; } int main() { struct Rectangle r; printf("Enter length and width: "); scanf("%f %f", &r.length, &r.width); modifyRectangle(&r); printf("\nUpdated Length = %.2f\n", r.length); printf("Updated Width = %.2f\n", r.width); return 0; }

Explanation

Passing the address of a structure allows a function to directly modify the original structure. The arrow operator is used for member access through the structure pointer.

Expected Output

Enter length and width: 10 5

Updated Length = 12.00

Updated Width = 8.00

Functions and Structures — Exercise 9

Problem

Write a C program to calculate the distance between two points using structures and a function.

Program

#include <stdio.h> #include <math.h> struct Point { float x; float y; }; float distance(struct Point p1, struct Point p2) { float dx = p2.x - p1.x; float dy = p2.y - p1.y; return sqrt(dx * dx + dy * dy); } int main() { struct Point p1, p2; float result; printf("Enter coordinates of first point: "); scanf("%f %f", &p1.x, &p1.y); printf("Enter coordinates of second point: "); scanf("%f %f", &p2.x, &p2.y); result = distance(p1, p2); printf("Distance = %.2f\n", result); return 0; }

Explanation

Two structure variables representing points are passed to the distance() function. The function uses their coordinates to calculate the distance.

Expected Output

Enter coordinates of first point: 0 0

Enter coordinates of second point: 3 4

Distance = 5.00

Functions and Structures — Exercise 10

Problem

Write a C program to create a student result system using structures and functions. The program should calculate total, average and grade.

Program

#include <stdio.h> struct Student { int roll; char name[50]; int math; int cProgramming; int computer; }; int totalMarks(struct Student s) { return s.math + s.cProgramming + s.computer; } char calculateGrade(float average) { if (average >= 90) return 'A'; else if (average >= 75) return 'B'; else if (average >= 60) return 'C'; else if (average >= 50) return 'D'; else return 'F'; } void displayResult(struct Student s) { int total; float average; char grade; total = totalMarks(s); average = total / 3.0; grade = calculateGrade(average); printf("\n=============================\n"); printf(" STUDENT RESULT\n"); printf("=============================\n"); printf("Roll : %d\n", s.roll); printf("Name : %s\n", s.name); printf("Total : %d\n", total); printf("Average : %.2f\n", average); printf("Grade : %c\n", grade); printf("=============================\n"); } int main() { struct Student s; printf("Enter roll number: "); scanf("%d", &s.roll); printf("Enter name: "); scanf("%49s", s.name); printf("Enter marks in Mathematics, C Programming and Computer: "); scanf("%d %d %d", &s.math, &s.cProgramming, &s.computer); displayResult(s); return 0; }

Explanation

This example combines several concepts: passing a structure to a function, returning a value from a function, using multiple functions, and processing structure members to generate a final result.

Expected Output

Enter roll number: 101

Enter name: Priya

Enter marks in Mathematics, C Programming and Computer: 92 88 95

=============================

STUDENT RESULT

=============================

Roll : 101

Name : Priya

Total : 275

Average : 91.67

Grade : A

=============================