CO110 Computer Programming10
CO110 Computer Programming10
After the above structure has been defined we can use struct person in
the same manner as we can use int, float, char , double.
We can also combine definition and variable declaration in one single
statement in the below fashion.
struct person
{
char name[50];
int id_no;
float salary;
} per1;
We can use the keyword typedef to rename the struct person data type.
typedef struct
{ typedef is used to create an
. ..; alias for a datatype, variable etc
} Person;
Now we can use the statement
Person per1, per2;
Accessing members of a structure
There are two types of operators used for accessing
members of a structure.
1. Dot operator(.)
2. Structure pointer operator(->) ( using pointer concept)
struct person
{
char name[20]; Note struct day definition is within
int id_no; struct person definiton.
float salary;
Also we have declared a variable of type
struct day struct day named “dob” .
{
int date;
Here we access the fields in the same manner as previously
int month; p1.dob.date;
int year; p1.dob.month;
}dob; p1.dob.year;
}p1;
Structure Initialization
Initializing a structure is the task of giving values to
the members of the structure.
This can be done in 2 manners
1. At Compile time
Here values are given to the structure members by the
programmer in the code itself. Here the values are harcoded.
2. At Run time.
Here values are given to the structure members by the user using
appropriate printf() and scanf() functions. That is the values are
not hardcoded.
Structure Initialization
1. At Compile Time
Consider the example
struct person Here the per1 and per2 structure
variables are given values in the
{ given syntax
char name[50]; 1. = operator between values and
structure variable.
int id_no; 2. enclosed within flower brackets
3. Seperated by a comma
float salary; 4. Terminated by a semicolon.
} per1 = { “mahesh”, 250, 5000.70},
per2= { “suresh”, 300, 5001.00};
Structure initialization
In the previous example the variable initialization was with along with
the structure definition.
struct person Here the per1 and per2 structure
{ variables are given values not
char name[50]; during the structure definition.
int id_no;
float salary; But after the definition in main
}; function. This creates a local
structure variable.
struct person per1= { “mahesh”,250, 5000.70}; E.g per2
void main()
{ Also the initialization can be done
struct person per2= { “suresh”, 300, 5001.00}; before main which will create a
- - - -- -- - -; global structure variable.
- - - - -- --- ---- --; E.g per1
}
Structure Program
Member initialization at Run Time
#include<stdio.h> scanf(“ %d”, &per[i].id_no);
struct person scanf(“ %f”,&per[i].salary);
{ char name[50]; }
int id_no;
float salary; printf(“The persons details entered are: \n “);
} per[10]; printf(“ Name \t Id \t Salary\n”);
void main() for(i=0; i< size; i++)
{ int size; {
printf(“Enter the number of persons: “); printf(“%s \t”, per[i].name);
scanf(“ %d”,&size); printf(“%d”\t, per[i].id_no);
printf(“Enter the persons details: \n “); printf(“%f \t”,per[i].salary);
for(i=0; i< size; i++) }
{ }
gets( per[i].name);
Unions
Unions are similar to structures syntactically.
union person Union members occupy the same
{ memory location.
char name[50];
int id_no; In a union, the memory allocated will be
float salary; equal to the size of the largest member.
}; E.g: Here Union size = 50, whereas
structure size = 58
By using Union we can
save a lot of memory In a union, only one member will be
which otherwise is active at a time.
unutilized.
Example
struct person
{ unsigned int age : 7;
int gender : 1;
};
Bitfields Program
#include<stdio.h> Output :
struct The code will compile with
{ unsigned int age : 3; warning and will give the
} Age; following output
}
printf(“The student details of Section S1 are: \n “);
printf(“ Name \t Roll No \t Marks1 \t Marks2 \t Marks3
\t Marks4 \t Marks5 \t Total \t Grade\n”);
Program to find grade of students in a class
for(i=0; i< class_strength ; i++)
{ printf(“%s\t %d\t %d\t %d\t %d\t %d\t %d\t %d\t
%c\n” , S1[i].name, S1[i].Roll_no,
S1[i].marks[0], S1[i].marks[1],
S1[i].marks[2], S1[i].marks[3],
S1[i].marks[4], S1[i].total, S1[i].grade );
}
}
Program to display the student list in descending
order total marks scored
Shyam 2 80 70 …. 520 A
Ram 1 50 45 …. . 348 C
Name Roll No Marks1 Marks2 Marks3 Marks4 Marks5 Total Grade
Rama Shasrty 12344 89 90 78 83 70 410 B
Shlipa Rani 11234 56 34 50 67 64 271 D
Mansoor Dsouza 13456 78 12 78 56 99 323 C
Raveena Yasmeen 10345 89 45 67 89 65 355 C
Ramesh Babu 10444 39 45 17 19 35 155 F
Robert Yashoda 17565 99 95 97 99 96 486 A
Udyan Basu 10345 89 45 67 89 65 355 C
}
printf(“The student details of Section S1 are: \n “);
printf(“ Name \t Roll No \t Marks1 \t Marks2 \t Marks3
\t Marks4 \t Marks5 \t Total \t Grade\n”);
Program to find grade of students in a class
for(i=0; i< class_strength ; i++)
{ printf(“%s\t %d\t %d\t %d\t %d\t %d\t %d\t %d\t
%c\n” , S1[i].name, S1[i].Roll_no,
S1[i].marks[0], S1[i].marks[1],
S1[i].marks[2], S1[i].marks[3],
S1[i].marks[4], S1[i].total, S1[i].grade);
}
}
Program to find grade of students in a class