C Programming • Structure and Union
C Programming / Introduction — Exercises

Introduction — Exercises

Practical 4 Structure and Union

Write a C program to create a simple structure named Student containing roll number, name and marks. Read and display the student's details.

Practical / Solution

Structure & Union — Exercise 1

Problem

Write a C program to create a simple structure named Student containing roll number, name and marks. Read and display the student's details.

Program

#include <stdio.h> struct Student { int roll; char name[50]; float marks; }; int main() { 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); // Display student details printf("\nStudent Details:\n"); printf("Roll Number = %d\n", s.roll); printf("Name = %s\n", s.name); printf("Marks = %.2f\n", s.marks); return 0; }

Explanation

A structure is a user-defined data type that can group variables of different data types under one name. Here, the Student structure groups roll number, name and marks.

Expected Output

Enter roll number: 101

Enter name: Rahul

Enter marks: 82.5

Student Details:

Roll Number = 101

Name = Rahul

Marks = 82.50

Structure & Union — Exercise 2

Problem

Write a C program to create a structure named Employee containing employee ID, name and salary, and display the stored information.

Program

#include <stdio.h> struct Employee { int id; char name[50]; float salary; }; int main() { struct Employee emp; printf("Enter employee ID: "); scanf("%d", &emp.id); printf("Enter name: "); scanf("%49s", emp.name); printf("Enter salary: "); scanf("%f", &emp.salary); // Display employee information printf("\nEmployee Details:\n"); printf("ID = %d\n", emp.id); printf("Name = %s\n", emp.name); printf("Salary = %.2f\n", emp.salary); return 0; }

Explanation

A structure can combine related information belonging to one entity. Each member is accessed using the dot operator.

Expected Output

Enter employee ID: 501

Enter name: Amit

Enter salary: 45000

Employee Details:

ID = 501

Name = Amit

Salary = 45000.00

Structure & Union — Exercise 3

Problem

Write a C program to store the details of a book using a structure containing book ID, title and price.

Program

#include <stdio.h> struct Book { int id; char title[100]; float price; }; int main() { struct Book book; printf("Enter book ID: "); scanf("%d", &book.id); printf("Enter title: "); scanf("%99s", book.title); printf("Enter price: "); scanf("%f", &book.price); // Display book details printf("\nBook Details:\n"); printf("Book ID = %d\n", book.id); printf("Title = %s\n", book.title); printf("Price = %.2f\n", book.price); return 0; }

Explanation

A structure is useful for representing a real-world object whose properties may have different data types.

Expected Output

Enter book ID: 101

Enter title: CProgramming

Enter price: 450

Book Details:

Book ID = 101

Title = CProgramming

Price = 450.00

Structure & Union — Exercise 4

Problem

Write a C program to calculate the total and average marks of a student using a structure.

Program

#include <stdio.h> struct Student { char name[50]; int marks1; int marks2; int marks3; }; int main() { struct Student s; int total; float average; printf("Enter student name: "); scanf("%49s", s.name); printf("Enter marks of three subjects: "); scanf("%d %d %d", &s.marks1, &s.marks2, &s.marks3); // Calculate total and average total = s.marks1 + s.marks2 + s.marks3; average = total / 3.0; printf("\nStudent = %s\n", s.name); printf("Total = %d\n", total); printf("Average = %.2f\n", average); return 0; }

Explanation

Structure members can be used directly in calculations. Here, the marks stored inside the structure are added to calculate the total and average.

Expected Output

Enter student name: Neha

Enter marks of three subjects: 70 80 90

Student = Neha

Total = 240

Average = 80.00

Structure & Union — Exercise 5

Problem

Write a C program to store the details of a rectangle using a structure and calculate its area and perimeter.

Program

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

Explanation

Structure members can represent the properties of a real-world object. Here, length and width are grouped inside the Rectangle structure.

Expected Output

Enter length and width: 10 5

Area = 50.00

Perimeter = 30.00

Structure & Union — Exercise 6

Problem

Write a C program to compare the marks of two students using a structure and display the student with higher marks.

Program

#include <stdio.h> struct Student { char name[50]; float marks; }; int main() { struct Student s1, s2; printf("Enter first student's name and marks: "); scanf("%49s %f", s1.name, &s1.marks); printf("Enter second student's name and marks: "); scanf("%49s %f", s2.name, &s2.marks); // Compare marks if (s1.marks > s2.marks) printf("%s has higher marks.\n", s1.name); else if (s2.marks > s1.marks) printf("%s has higher marks.\n", s2.name); else printf("Both students have equal marks.\n"); return 0; }

Explanation

Separate structure variables can store information about different students. Their individual members can then be compared like normal variables.

Expected Output

Enter first student's name and marks: Rahul 78

Enter second student's name and marks: Amit 85

Amit has higher marks.

Structure & Union — Exercise 7

Problem

Write a C program to store date information using a structure containing day, month and year, and display the date.

Program

#include <stdio.h> struct Date { int day; int month; int year; }; int main() { struct Date date; printf("Enter day, month and year: "); scanf("%d %d %d", &date.day, &date.month, &date.year); // Display date printf("Date = %02d/%02d/%d\n", date.day, date.month, date.year); return 0; }

Explanation

A structure can group several related values into one logical unit. The Date structure groups day, month and year.

Expected Output

Enter day, month and year: 4 9 2026

Date = 04/09/2026

Structure & Union — Exercise 8

Problem

Write a C program to store the details of a product using a structure and calculate the total cost for a given quantity.

Program

#include <stdio.h> struct Product { int id; char name[50]; float price; int quantity; }; int main() { struct Product p; float total; printf("Enter product ID: "); scanf("%d", &p.id); printf("Enter product name: "); scanf("%49s", p.name); printf("Enter price: "); scanf("%f", &p.price); printf("Enter quantity: "); scanf("%d", &p.quantity); // Calculate total cost total = p.price * p.quantity; printf("\nProduct Details:\n"); printf("ID = %d\n", p.id); printf("Name = %s\n", p.name); printf("Total Cost = %.2f\n", total); return 0; }

Explanation

A structure allows multiple properties of a product to be stored together. The stored members can then be used in calculations.

Expected Output

Enter product ID: 201

Enter product name: Keyboard

Enter price: 800

Enter quantity: 3

Product Details:

ID = 201

Name = Keyboard

Total Cost = 2400.00

Structure & Union — Exercise 9

Problem

Write a C program to store the details of a student using a structure and determine whether the student has passed or failed based on marks.

Program

#include <stdio.h> struct Student { int roll; char name[50]; float marks; }; int main() { 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); // Check result using structure member if (s.marks >= 50) printf("%s has passed.\n", s.name); else printf("%s has failed.\n", s.name); return 0; }

Explanation

Structure members can be used directly in decision-making statements. Here, the student's marks member is used to determine the result.

Expected Output

Enter roll number: 105

Enter name: Priya

Enter marks: 68

Priya has passed.

Structure & Union — Exercise 10

Problem

Write a C program to create a structure named Student containing roll number, name and marks, and display the complete student record in a formatted manner.

Program

#include <stdio.h> struct Student { int roll; char name[50]; float marks; }; int main() { 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); // Display complete record printf("\n-------------------------\n"); printf(" STUDENT RECORD\n"); printf("-------------------------\n"); printf("Roll Number : %d\n", s.roll); printf("Name : %s\n", s.name); printf("Marks : %.2f\n", s.marks); printf("-------------------------\n"); return 0; }

Explanation

This program brings together the basic structure concepts covered in this section: structure definition, structure variable creation, member access using the dot operator, input and formatted output.

Expected Output

Enter roll number: 101

Enter name: Rohit

Enter marks: 88.5

-------------------------

STUDENT RECORD

-------------------------

Roll Number : 101

Name : Rohit

Marks : 88.50

-------------------------