BIT102 ยท 2077 ยท 10 marks
How structure is different from union? Write a program to store and display basic information (roll, name, address, email, and phone) of students of using a structure.
How structure is different from union? Write a program to store and display basic information (roll, name, address, email, and phone) of students of using a structure.[10]
C ProgrammingChapter 9: Structures and UnionsDifferences between structure and union
Structure vs Union in C
Difference Between Structure and Union
| Feature | Structure | Union |
|---|---|---|
| Keyword | struct | union |
| Memory Allocation | Each member gets its own separate memory | All members share the same memory location |
| Total Size | Sum of sizes of all members | Size of the largest member only |
| Data Storage | All members can store values simultaneously | Only one member can hold a value at a time |
| Access | All members can be accessed at any time | Only the last written member gives valid data |
| Usage | When all data fields are needed together | When only one field is needed at a time (saves memory) |
Example of size difference:
struct S { int a; float b; char c; };
// Size = 4 + 4 + 1 = 9 bytes (approx, with padding)
union U { int a; float b; char c; };
// Size = 4 bytes (size of largest member: int or float)
C Program to Store and Display Student Information Using Structure
#include <stdio.h>
#include <string.h>
// Define the structure for student information
struct Student {
int roll;
char name[50];
char address[100];
char email[50];
char phone[15];
};
int main() {
int i, n;
printf("Enter the number of students: ");
scanf("%d", &n);
// Declare an array of structures
struct Student s[n];
// Input student information
for (i = 0; i < n; i++) {
printf("\n--- Enter details for Student %d ---\n", i + 1);
printf("Roll Number : ");
scanf("%d", &s[i].roll);
printf("Name : ");
// Clear input buffer before reading string
getchar();
fgets(s[i].name, sizeof(s[i].name), stdin);
s[i].name[strcspn(s[i].name, "\n")] = '\0'; // Remove newline
printf("Address : ");
fgets(s[i].address, sizeof(s[i].address), stdin);
s[i].address[strcspn(s[i].address, "\n")] = '\0';
printf("Email : ");
fgets(s[i].email, sizeof(s[i].email), stdin);
s[i].email[strcspn(s[i].email, "\n")] = '\0';
printf("Phone : ");
fgets(s[i].phone, sizeof(s[i].phone), stdin);
s[i].phone[strcspn(s[i].phone, "\n")] = '\0';
}
// Display student information
printf("\n\n========== Student Information ==========\n");
for (i = 0; i < n; i++) {
printf("\nStudent %d:\n", i + 1);
printf(" Roll Number : %d\n", s[i].roll);
printf(" Name : %s\n", s[i].name);
printf(" Address : %s\n", s[i].address);
printf(" Email : %s\n", s[i].email);
printf(" Phone : %s\n", s[i].phone);
}
return 0;
}
Sample Output
Enter the number of students: 2
--- Enter details for Student 1 ---
Roll Number : 101
Name : Ram Sharma
Address : Kathmandu
Email : [email protected]
Phone : 9841000001
--- Enter details for Student 2 ---
Roll Number : 102
Name : Sita Thapa
Address : Pokhara
Email : [email protected]
Phone : 9841000002
========== Student Information ==========
Student 1:
Roll Number : 101
Name : Ram Sharma
Address : Kathmandu
Email : [email protected]
Phone : 9841000001
Student 2:
Roll Number : 102
Name : Sita Thapa
Address : Pokhara
Email : [email protected]
Phone : 9841000002
Key Points to Remember
struct Studentdefines a template/blueprint for student data.- The dot operator (
.) is used to access structure members. - An array of structures (
struct Student s[n]) allows storing multiple students. - Structure is preferred here because all fields are needed simultaneously.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.