1 of 4
SCHOOL OF ENGINEERING
REFLECTIVE JOURNALING
Name of the Student: B. Shashank Reddy Academic Year: 2024-28
Student Registration Number: 241U1R1025 Year & Term: I & II
Study Level: UG Class & Section: CSE – 1A
Name of the Course: C Programming Name of the Instructor: Mr. K. Ravikanth
Name of the Assessment: free writing - 4 Date of Submission: 28 /01/ 24
FREE WRITING - 4
Structures and unions are fundamental components of the C programming language,
providing a robust framework for handling complex data types. They allow
programmers to encapsulate different data types into a single unit, facilitating the
organization and manipulation of related data. Mastering the use of structures and
unions is essential for writing clean, efficient, and maintainable code, which is crucial
in software development.
Structures
A structure in C is a user-defined data type that allows the combination of various data
types into a single entity. Each element within a structure is referred to as a member,
and these members can be of different data types, including integers, floats, characters,
and even other structures. This flexibility makes structures particularly useful for
modeling real-world entities, such as students, products, or vehicles. To illustrate the
concept of structures, let’s define a structure to model a student. We can create a
structure called Student that includes members for the student’s name, roll number, age,
and GPA.
• char name[50]: This member is an array of characters that can hold up to 49
characters plus a null terminator, allowing us to store the student's name.
• int rollNumber: This member is an integer that stores the unique roll number
assigned to the student.
2 of 4
• int age: This member is also an integer that represents the student's age.
• float gpa: This member is a floating-point number that stores the student's Grade
Point Average (GPA).
• We define a structure named Student with four
members: name, rollNumber, age, and gpa.
• We create a variable student1 of type struct Student.
• We use the strcpy function to assign a name to the name member and directly
• assign values to the other members.
• Finally, we print the details of the student using printf.
Unions are similar to structures but with a significant difference: all members of a
union share the same memory location. This means that a union can store different data
types, but only one member can hold a value at any given time. This feature makes
unions memory efficient, as they utilize the size of the largest member.
To illustrate the concept of unions, let’s define a union that can hold different types of
data. We can create a union called Measurement that can store either an integer for
length, a float for weight, or a character for a unit.
• int length: This member can store an integer value representing length.
• float weight: This member can store a floating-point value representing weight.
• char unit: This member can store a character representing the unit of
measurement
• We define a union named Measurement that can hold an integer, a float, or a
character.
• We create a variable measure of type union Measurement.
• We assign values to the length, weight, and unit members sequentially and print
them.
Advantages of unions
1. Memory Efficiency: Unions are memory efficient because they use the size of
3 of 4
2. the largest member, allowing for the storage of different data types without
requiring additional memory.
3. Flexibility: Unions provide flexibility in handling different types of data, making
them useful in scenarios where the type of data may vary.
4. Simplified Code: By using unions, programmers can simplify code that needs to
handle multiple data types without creating separate variables for each type.