CH 10 Struct and Union
CH 10 Struct and Union
struct lib_books
{
char title[20];
char author[15];
int pages;
float price;
};
4
DECLARING STRUCTURE VARIABLE
To access structure item, we require to create
structure variable (object).
A structure variable declaration is similar to
the declaration of variables of any other data
types.
It includes the following elements:
1. The keyword struct.
2. The structure tag name.
3. List of variable names separated by names.
4. A terminating semicolon.
Syntax:- 5
6
DECLARING STRUCTURE VARIABLE
We can also combine both template declaration
and variables declaration in one
Statement, the declaration
struct lib_books
{
char title[20];
char author[15];
int pages;
float price;
} book1,book2,book3;
is valid. 7
DECLARING STRUCTURE VARIABLE
The use of tag name is optional for example
struct
{
…
…
…
} book1, book2, book3 ;
For example:
Book1.price
strcpy(book1.title,”basic”);
strcpy(book1.author,”Balagurusamy”);
book1.pages=250;
book1.price=285.0; 10
Write a C program that demonstrates the use of
structure.
#include<stdio.h> scanf("%s",s2.name);
#include<conio.h> printf("Enter the roll number of
struct student student2:");
{ scanf("%d",&s2.num);
char name[20]; printf("Enter the marks of student2:");
int num; scanf("%f",&s2.marks);
float marks; printf("Detail of Students\n");
};
int main() printf("______________________________
{ ___\n");
struct student s1,s2; printf("%s\n",s1.name);
printf("Enter the name of student1:"); printf("%d\n",s1.num);
scanf("%s",s1.name); printf("%f\n",s1.marks);
printf("Enter the roll number of
student1:"); printf("______________________________
scanf("%d",&s1.num); ____\n");
printf("Enter the marks of student1:"); printf("%s\n",s2.name);
scanf("%f",&s1.marks); printf("%d\n",s2.num);
printf("Enter the name of student2:"); printf("%f\n",s2.marks);
return 0; 11
}
Write a program to create structure name student. Enter the
student’s name, Roll No, and marks of five subjects. Find out the
percentage and also define the grade of the student.
#include<stdio.h> if(s1.per>=90 && s1.per<=100)
#include<string.h> strcpy(s1.grade,"A+");
else if(s1.per>79 && s1.per<90)
struct student strcpy(s1.grade,"A");
{ else if(s1.per>69 && s1.per<80)
char name[50]; strcpy(s1.grade,"B+");
char rollno[50]; else if(s1.per>59 && s1.per<70)
int m1,m2,m3,m4,m5; strcpy(s1.grade,"B");
float per; else if(s1.per>49 && s1.per<60)
char grade[3]; strcpy(s1.grade,"C+");
}; else if(s1.per>39 && s1.per<50)
strcpy(s1.grade,"C");
int main() else
{ strcpy(s1.grade,"F");
struct student s1;
printf("Enter the name, rollno & marks of five printf("\nName=%s\nRoll
subjects"); No=%s\nPercentage=%f\nGrade=
scanf("%s%s%d%d%d%d%d",s1.name,s1.rollno,& %s",s1.name,s1.rollno,s1.per,s1.gra
s1.m1,&s1.m2,&s1.m3,&s1.m4,&s1.m5); de);
s1.per = printf("\nSize of struct is
(float)(s1.m1+s1.m2+s1.m3+s1.m4+s1.m5)/5; %d",sizeof(s1)); 12
return 0;
}
COMPARING AND COPYING STRUCTURE VARIABLES
STRUCTURE INITIALIZATION
Example,
struct student
{
int weight;
float height;
} s3 = {70,181.57};
14
ARRAYS OF STRUCTURES
We may declare an array of structures, each
element of the array representing a structure
variable.
For example,