1. Structure in C
Definition
A structure is a user-defined data type that allows us to store different types of data under a single name.
For example, a student’s record contains:
- Roll Number (int)
- Name (char array)
- Marks (float)
Since these are different data types, we use a structure.
Syntax
struct Student
{
int roll;
char name[30];
float marks;
};
struct→ Keyword used to define a structure.Student→ Structure name.roll,name,marks→ Members of the structure.
Example Program
#include <stdio.h>
struct Student
{
int roll;
char name[30];
float marks;
};
int main()
{
struct Student s1;
s1.roll = 101;
strcpy(s1.name, "Rahul");
s1.marks = 89.5;
printf("Roll Number : %d\n", s1.roll);
printf("Name : %s\n", s1.name);
printf("Marks : %.2f\n", s1.marks);
return 0;
}
Note: To use strcpy(), include the header file:
#include <string.h>
Output
Roll Number : 101
Name : Rahul
Marks : 89.50
Memory Representation of Structure
Structure Student
+----------------+
| Roll = 101 |
+----------------+
| Name = Rahul |
+----------------+
| Marks = 89.5 |
+----------------+
Each member gets its own memory.
Advantages of Structure
- Stores different data types together.
- Represents real-world entities (Student, Employee, Book, etc.).
- Makes programs organized and easier to manage.
- Multiple structure variables can be created.
2. Union in C
Definition
A union is also a user-defined data type, but all members share the same memory location.
Only one member can hold a valid value at a time.
Syntax
union Data
{
int i;
float f;
char ch;
};
Example Program
#include <stdio.h>
union Data
{
int i;
float f;
char ch;
};
int main()
{
union Data d;
d.i = 100;
printf("Integer = %d\n", d.i);
d.f = 12.5;
printf("Float = %.1f\n", d.f);
d.ch = 'A';
printf("Character = %c\n", d.ch);
return 0;
}
Output
Integer = 100
Float = 12.5
Character = A
Important: After assigning d.ch = 'A';, the previous values of d.i and d.f are overwritten because all members share the same memory.
Memory Representation of Union
Union Data
+----------------+
| Shared Memory |
| int |
| float |
| char |
+----------------+
Only one value is stored at a time.
Difference Between Structure and Union
| Structure | Union |
|---|---|
| Each member has separate memory. | All members share the same memory. |
| All members can store values simultaneously. | Only one member can store a valid value at a time. |
| Memory size is the sum of all members (plus possible padding). | Memory size is equal to the size of the largest member. |
| More memory is required. | Less memory is required. |
| Used when all data members are needed together. | Used when only one member is needed at a time. |
Real-Life Example
Structure
A Student Record stores:
- Roll Number
- Name
- Marks
All these details exist together, so Structure is the correct choice.
Union
A Payment Method may be:
- Credit Card
- UPI
- Cash
At any moment, only one payment method is used, so Union is more suitable.
Exam Definition
Structure
A structure is a user-defined data type that groups variables of different data types under a single name. Each member has its own memory location.
Union
A union is a user-defined data type in which all members share the same memory location. Only one member can contain a valid value at a time.
