(Grade XII)Part 6: Structure & Union
Structure
Structure
is a user-defined data type that groups variables of different data types under
one name.
example
struct
student
{
char name[20];
int roll;
float marks;
};
Structure
vs Union (very imp):
|
Structure |
Union |
|
Keyword
struct |
Keyword
union |
|
Memory
= sum of all members |
Memory
= size of the largest member |
|
Each
member has its own separate memory |
All
members share the same memory location |
|
Suitable
for storing full, unrelated records |
Suitable
for memory-saving / hardware-level use |
|
Example:
struct sample{int a; float p; char n[10];} a; |
Example:
union sample{int a; float p; char n[10];} a; |
Structure
vs Array (very imp):
|
Array |
Structure |
|
An array stores multiple values of
the same data type. |
A structure stores different types
of data under one name. |
|
All elements of an array must have
the same data type. |
Members of a structure can have
different data types. |
|
Array elements are accessed using
an index number. |
Structure members are accessed
using the dot (.) operator. |
|
Example: int marks[5]; |
Example: struct student { char
name[20]; int roll; }; |
|
It is mainly used to store a
collection of similar data. |
It is mainly used to store
different information about one entity. |
Programs
1. Write a program to input the name, address
and roll number of a student using structure and display it.
#include<stdio.h>
struct
student
{
char name[50];
char address[50];
int roll;
};
int
main()
{
struct student s;
printf("Enter name.: ");
scanf("%s", s.name);
printf("Enter address.: ");
scanf("%s",
s.address);
printf("Enter roll no.: ");
scanf("%d", &s.roll);
printf("Name: %s\nAddress: %s\nRoll:
%d\n", s.name, s.address, s.roll);
return 0;
}
2.
Write a program to input name, roll and registration no of 10 students using
structure and display in proper format.
#include
<stdio.h>
struct
students
{
char name[100];
int roll;
int regno;
}
var[10];
int
main()
{
int i;
for(i = 0; i < 10; i=i+1)
{
printf("Enter name\n");
scanf("%s", var[i].name);
printf("Enter roll \n");
scanf("%d", &var[i].roll);
printf("Enter regno\n");
scanf("%d", &var[i].regno);
}
for(i = 0; i < 10; i=i+1)
{
printf("name=%s roll=%d regno=%d\n",
var[i].name, var[i].roll,
var[i].regno);
}
return 0;
}
3.Write
a program to input employee id, name, address and post of 20 employees and
display them properly.
#include<stdio.h>
struct
employee
{
int id;
char name[50];
char address[50];
char post[50];
}emp[20];
int
main()
{
int i;
for(i=0;i<20;i++)
{
printf("Enter id");
scanf("%d",&emp[i].id);
printf("Enter name");
scanf("%s",emp[i].name);
printf("Enter address");
scanf("%s",emp[i].address);
printf("Enter post");
scanf("%s",emp[i].post);
}
printf("\nEmployee records:\n");
for(i=0;i<20;i++)
printf("%d\t%s\t%s\t%s\n", emp[i].id, emp[i].name,
emp[i].address, emp[i].post);
return 0;
}
4.
Write a C program that uses structures
to represent details of five books (title, author, publisher and price) and
prints them out.
#include<stdio.h>
struct
book
{
char b_title[100];
char b_author[100];
char b_publisher[100];
float b_price;
}a[5];
int
main()
{
int i;
for(i=0;i<=5;i++)
{
printf("Enter book title\n");
scanf("%s",a[i].b_title);
printf("Enter author\n");
scanf("%s",a[i].b_author);
printf("Enter publisher\n");
scanf("%s",a[i].b_publisher);
printf("Enter price\n");
scanf("%f",&a[i].b_price);
}
printf("The records are:\n");
for(i=0;i<=5;i++)
printf("Title=%s Author=%s Publisher=%s
Price=%.2f\n",
a[i].b_title, a[i].b_author,
a[i].b_publisher, a[i].b_price);
return 0;
}
5.
Write a C program that reads the account_number, name and address of ten
customers from users and displays the account_number, name and address of these
customers using Array and structure.
#include<stdio.h>
struct
customer
{
int account_number;
char name[50];
char address[50];
}cust[10];
int
main()
{
int i;
for(i=0;i<10;i++)
{
printf("Enter account number, name
and address of customer %d\n", i+1);
scanf("%d",&cust[i].account_number);
scanf("%s",cust[i].name);
scanf("%s",cust[i].address);
}
printf("\nCustomer Records:\n");
for(i=0;i<10;i++)
printf("A/C No: %d\tName:
%s\tAddress: %s\n",
cust[i].account_number,
cust[i].name, cust[i].address);
return 0;
}
6.
Write a program to input name, roll and marks of 3 subjects for n students
using structure, and display name with percentage.
#include<stdio.h>
struct
student
{
char name[50];
int roll;
float m1,m2,m3;
float percentage;
};
int
main()
{
int n,i;
printf("Enter number of students:
");
scanf("%d",&n);
struct student s[n];
for(i=0;i<n;i++)
{
printf("Enter name, roll, marks in
3 subjects\n");
scanf("%s",s[i].name);
scanf("%d",&s[i].roll);
scanf("%f %f
%f",&s[i].m1,&s[i].m2,&s[i].m3);
s[i].percentage =
(s[i].m1+s[i].m2+s[i].m3)/3;
}
printf("\nName\tRoll\tPercentage\n");
for(i=0;i<n;i++)
printf("%s\t%d\t%.2f\n",
s[i].name, s[i].roll, s[i].percentage);
return 0;
}
7.
Write a C program to create a structure named student
with members name, class, roll number and marks. Store
the details of 5 students in a file named student.txt
and display all the records stored in the file.
#include<stdio.h>
struct
student
{
char name[20];
int cls;
int roll;
int marks;
};
int main()
{
struct student s;
FILE *fptr;
int i;
fptr = fopen("student.txt",
"w");
for(i=1; i<=5; i++)
{
printf("Enter name, class, roll
and marks: ");
scanf("%s %d %d %d", s.name,
&s.cls, &s.roll, &s.marks);
fprintf(fptr, "%s %d %d
%d\n",
s.name, s.cls, s.roll, s.marks);
}
fclose(fptr);
fptr = fopen("student.txt",
"r");
printf("\nName\tClass\tRoll\tMarks\n");
while(fscanf(fptr, "%s %d %d %d",
s.name, &s.cls, &s.roll,
&s.marks) != EOF)
{
printf("%s\t%d\t%d\t%d\n",
s.name, s.cls, s.roll, s.marks);
}
fclose(fptr);
return 0;
}
8.
Write a C program to add the details of an employee to an existing file named employee.txt.
The details should include name, ID and salary. Display all
the employee records after adding the new record.
#include<stdio.h>
struct
employee
{
char name[20];
int id;
float salary;
};
int main()
{
struct employee e;
FILE *fptr;
fptr = fopen("employee.txt",
"a");
printf("Enter employee name, ID and
salary: ");
scanf("%s %d %f", e.name,
&e.id, &e.salary);
fprintf(fptr, "%s %d %.2f\n",
e.name, e.id, e.salary);
fclose(fptr);
fptr = fopen("employee.txt",
"r");
printf("\nName\tID\tSalary\n");
while(fscanf(fptr, "%s %d %f",
e.name, &e.id, &e.salary) !=
EOF)
{
printf("%s\t%d\t%.2f\n",
e.name, e.id, e.salary);
}
fclose(fptr);
return 0;
}
9. Write a C program to store the details of students including name,
class, roll number and marks in a file named student.txt.
Read the records from the file and display only those students who have
obtained 40 or more marks.
#include<stdio.h>
struct
student
{
char name[20];
int cls;
int
roll;
int marks;
};
int main()
{
struct student s;
FILE *fptr;
int i;
fptr = fopen("student.txt",
"w");
for(i=1; i<=5; i++)
{
printf("Enter name, class, roll
and marks: ");
scanf("%s %d %d %d",
s.name, &s.cls, &s.roll,
&s.marks);
fprintf(fptr, "%s %d %d
%d\n",
s.name, s.cls, s.roll,
s.marks);
}
fclose(fptr);
fptr = fopen("student.txt",
"r");
printf("\nName\tClass\tRoll\tMarks\n");
while(fscanf(fptr, "%s %d %d %d",
s.name, &s.cls, &s.roll,
&s.marks) != EOF)
{
if(s.marks >= 40)
{
printf("%s\t%d\t%d\t%d\n",
s.name, s.cls, s.roll,
s.marks);
}
}
fclose(fptr);
return 0;
}
Union
1.
Write a C program to define a union named student with members roll and marks.
Input and display the roll number and marks.
#include
<stdio.h>
union
student
{
int roll;
float marks;
};
int
main()
{
union student s;
printf("Enter roll number: ");
scanf("%d", &s.roll);
printf("Roll = %d\n", s.roll);
printf("Enter marks: ");
scanf("%f", &s.marks);
printf("Marks = %.2f\n", s.marks);
return 0;
}
2.
Write a C program to demonstrate that members of a union share the same memory
location.
#include
<stdio.h>
union
sample
{
int a;
float b;
};
int
main()
{
union sample s;
s.a = 10;
printf("a = %d\n", s.a);
s.b = 20.5;
printf("b = %.2f\n", s.b);
return
0;
}
3.
Write a C program to create a union named employee with members id, salary and name.
Input and display the values one at a time.
#include
<stdio.h>
union
employee
{
int id;
float salary;
char name[20];
};
int
main()
{
union employee e;
printf("Enter employee ID: ");
scanf("%d", &e.id);
printf("ID = %d\n", e.id);
printf("Enter salary: ");
scanf("%f", &e.salary);
printf("Salary = %.2f\n", e.salary);
printf("Enter name: ");
scanf("%s", e.name);
printf("Name = %s\n", e.name);
return 0;
}