KEMBAR78
Structure&union | PPTX
Khanal Pralhad
Navodit College, Samakhusi, Nepal
Khanal Pralhad: pkhanal95.pk@gmail.com
Khanal Pralhad: pkhanal95.pk@gmail.com
Khanal Pralhad: pkhanal95.pk@gmail.com
Khanal Pralhad: pkhanal95.pk@gmail.com
Why structures?
• We know that an Array is a collection of similar data items such as integer, float, char etc. If more
number of data items of same data type are grouped, we normally use arrays.
• For example, the marks of 5 students can be stored using array as shown below:
• marks[5] = {80, 90, 45, 99, 100};
• For collection of dissimilar data types. For example, suppose we want to have the information
related to a student. We may be interested in:
♦ name of the student (string type)
♦ roll number (int type)
♦ average marks (float type)
• Note :
That the above information is a collection of various items of dissimilar data types.So, arrays
cannot be used (because, array is a collection of similar data types). This is where the structures
are used.
Khanal Pralhad: pkhanal95.pk@gmail.com
Structure and Union
• In C programming language, a structure is a collection of elements of
the different data type.
• The structure is used to create user-defined data type in the C
programming language.
• As the structure used to create a user-defined data type, the
structure is also said to be “user-defined data type in C”.
• In other words, a structure is a collection of non-homogeneous
elements. Using structure we can define new data types called user-
defined data types that holds multiple values of the different data
type.
Khanal Pralhad: pkhanal95.pk@gmail.com
A Structure is a collection of one or more variables of different data types grouped together under a
single name .
All these variables may contain data items of similar or dissimilar data types. Using these variables
each item of a structure can be selected.
Each variable in the structure represents an item and is called member or field of the structure. Each
field has a type.
To create structure in c, we use the keyword called "struct".
The general format or syntax of a structure is shown below:
struct tag_name {
type1 member1;
type2 member2;
……… ……………….
……… ……………….
type2 memberN;
} ;
Khanal Pralhad: pkhanal95.pk@gmail.com
Syntax: Structure
Khanal Pralhad: pkhanal95.pk@gmail.com
Example:Structure
struct Student
{
char stud_name[30];
int roll_number;
float percentage;
} ;
Struct student s1;
struct Student
{
char stud_name[30];
int roll_number;
float percentage;
} s1 ;
Khanal Pralhad: pkhanal95.pk@gmail.com
Structure Initialization
• The syntax of initializing structure variables is similar to that of arrays
i.e., all the elements will be enclosed within curly braces i.e., ‘{‘ and ‘}’
and are separated by commas.The syntax is shown below:
struct tag_name variable = {v1, v2, ...vn };
• struct employee {
char name[20];
int salary;
int id;
} a = {"Shivashankar", 10950, 2001};
Khanal Pralhad: pkhanal95.pk@gmail.com
Accessing Structure Member
-The members of a structure are separate entities and so should be processed individually.
-A member of a structure can be accessed by specifying the variable name followed by a
period (also called dot) which in turn is followed by the member name using the syntax
shown below:
variable.member
Consider the structure definition and initialization shown below:
struct employee {
char name[10];
float salary;
int id;
}
struct employee a = {“RAMA", 10950, 2001};
Khanal Pralhad: pkhanal95.pk@gmail.com
By specifying a.name we can access the string "RAMA".
By specifying a.salary we can access the value 10950.000000
By specifying a.id we can access the value of 2001
printf(“%sn”, a.name); RAMA
printf(“%fn”, a.salary); 10950.000000
printf(“%dn”, a.id); 2001
Khanal Pralhad: pkhanal95.pk@gmail.com
Example
struct Student
{
char stud_name[30];
int roll_number;
float percentage;
} stud_1 ;
void main(){
struct Student stud_2;
printf("Enter details of stud_1 : n");
printf("Name : ");
scanf("%s", stud_1.stud_name);
printf("Roll Number : ");
scanf("%d", &stud_1.roll_number);
printf("Percentage : ");
scanf("%f", &stud_1.percentage);
printf("Name of the Student : %sn",
stud_1.stud_name);
printf("Roll Number of the Student :
%in", stud_1.roll_number);
printf("Percentage of the Student :
%fn", stud_1.percentage);
}
Khanal Pralhad: pkhanal95.pk@gmail.com
Program :- To arrange student’s information in
ascending order using an array
Khanal Pralhad: pkhanal95.pk@gmail.com
struct student{
char name[25];
int rno;
int marks[3];
};
void main(){
struct student s;
int i;
float total=0,avg;
printf("Enter Student Name:");
gets(s.name);
printf("Enter Student Rollno:");
scanf("%d",&s.rno);
printf("ntttEnter Student Marks Detailsn");
for(i=0;i<3;i++){
printf("Enter Student Subject %d Marks: ",i+1);
scanf("%d",&s.marks[i]);
total= total+s.marks[i];}
avg=total/3;
printf("tt Student Detailsn");
printf("Student Name : ");
puts(s.name);
printf("nStudent Roll no: %dn",s.rno);
printf("ttStudent Marksn");
printf("tt------- -----nn");
for(i=0;i<3;i++)
printf("ttSubject %d : %dn",i+1,s.marks[i]);
printf("tt ====n");
printf("ttTotal : %fn",total);
printf("ttAverage : %fn",avg);
getch();
}
Khanal Pralhad: pkhanal95.pk@gmail.com
Union
Khanal Pralhad: pkhanal95.pk@gmail.com
UNION
• In C programming language, the union is a collection of elements of
the different data type.
• The union is used to create user-defined data type in the C
programming language. As the union used to create a user-defined
data type, the union is also said to be “user-defined data type in C”.
• In other words, the union is a collection of non-homogeneous
elements.
• Using union we can define new data types called user-defined data
types that holds multiple values of the different data type.
Khanal Pralhad: pkhanal95.pk@gmail.com
Syntax: Union
• The general format or syntax of a union is shown below:
union tag_name {
type1 member1;
type2 member2;
……… ……………….
……… ……………….
} ;
Khanal Pralhad: pkhanal95.pk@gmail.com
Example: Union
union Student
{
char stud_name[30];
int roll_number;
float percentage;
} ;
Khanal Pralhad: pkhanal95.pk@gmail.com
union Student
{
char stud_name[30];
int roll_number;
float percentage;
} stud_1 ; // while defining union
void main(){
union Student stud_2; // using union keyword
printf("Enter details of stud_1 : n");
printf("Name : ");
scanf("%s", stud_1.stud_name);
printf("Roll Number : ");
scanf("%d", &stud_1.roll_number);
printf("Percentage : ");
scanf("%f", &stud_1.percentage);
printf("Name of the Student : %sn",
stud_1.stud_name);
printf("Roll Number of the Student : %in",
stud_1.roll_number);
printf("Percentage of the Student : %fn",
stud_1.percentage);
}
Khanal Pralhad: pkhanal95.pk@gmail.com
Khanal Pralhad: pkhanal95.pk@gmail.com
Khanal Pralhad: pkhanal95.pk@gmail.com
• Typedef
typedef struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
} Book;
• Nested structure
Khanal Pralhad: pkhanal95.pk@gmail.com
typedef struct student
{
int roll_no;
char name[30];
int phone_number;
}st;
int main()
{
st p1, p2, p3;
p1.roll_no = 1;
strcpy(p1.name,"Brown");
p1.phone_number = 123443;
p2.roll_no = 2;
strcpy(p2.name,"Sam");
p2.phone_number = 1234567822;
p3.roll_no = 3;
strcpy(p3.name,"Addy");
p3.phone_number = 1234567844;
printf("First Studentn");
printf("roll_no : %dn", p1.roll_no);
printf("name : %sn", p1.name);
printf("phone_number : %dn", p1.phone_number);
printf("Second Studentn");
printf("roll_no : %dn", p2.roll_no);
printf("name : %sn", p2.name);
printf("phone_number : %dn", p2.phone_number);
printf("Third Studentn");
printf("roll_no : %dn", p3.roll_no);
printf("name : %sn", p3.name);
printf("phone_number : %dn", p3.phone_number);
return 0;
}
Khanal Pralhad: pkhanal95.pk@gmail.com
Assignments
1. WAP that takes the address and marks of 10 students .Sort data
according to marks in descending order and display.
2. WAP to take stud_roll,fname,lname of 10 students and display
them.
3. WAP to take stud_roll,fname,lname of 10 students and display
them on the basis of stud_roll number(descending order).
Khanal Pralhad: pkhanal95.pk@gmail.com
Khanal Pralhad: pkhanal95.pk@gmail.com
Khanal Pralhad: pkhanal95.pk@gmail.com

Structure&amp;union

  • 1.
    Khanal Pralhad Navodit College,Samakhusi, Nepal Khanal Pralhad: pkhanal95.pk@gmail.com
  • 2.
  • 3.
  • 4.
  • 5.
    Why structures? • Weknow that an Array is a collection of similar data items such as integer, float, char etc. If more number of data items of same data type are grouped, we normally use arrays. • For example, the marks of 5 students can be stored using array as shown below: • marks[5] = {80, 90, 45, 99, 100}; • For collection of dissimilar data types. For example, suppose we want to have the information related to a student. We may be interested in: ♦ name of the student (string type) ♦ roll number (int type) ♦ average marks (float type) • Note : That the above information is a collection of various items of dissimilar data types.So, arrays cannot be used (because, array is a collection of similar data types). This is where the structures are used. Khanal Pralhad: pkhanal95.pk@gmail.com
  • 6.
    Structure and Union •In C programming language, a structure is a collection of elements of the different data type. • The structure is used to create user-defined data type in the C programming language. • As the structure used to create a user-defined data type, the structure is also said to be “user-defined data type in C”. • In other words, a structure is a collection of non-homogeneous elements. Using structure we can define new data types called user- defined data types that holds multiple values of the different data type. Khanal Pralhad: pkhanal95.pk@gmail.com
  • 7.
    A Structure isa collection of one or more variables of different data types grouped together under a single name . All these variables may contain data items of similar or dissimilar data types. Using these variables each item of a structure can be selected. Each variable in the structure represents an item and is called member or field of the structure. Each field has a type. To create structure in c, we use the keyword called "struct". The general format or syntax of a structure is shown below: struct tag_name { type1 member1; type2 member2; ……… ………………. ……… ………………. type2 memberN; } ; Khanal Pralhad: pkhanal95.pk@gmail.com
  • 8.
    Syntax: Structure Khanal Pralhad:pkhanal95.pk@gmail.com
  • 9.
    Example:Structure struct Student { char stud_name[30]; introll_number; float percentage; } ; Struct student s1; struct Student { char stud_name[30]; int roll_number; float percentage; } s1 ; Khanal Pralhad: pkhanal95.pk@gmail.com
  • 10.
    Structure Initialization • Thesyntax of initializing structure variables is similar to that of arrays i.e., all the elements will be enclosed within curly braces i.e., ‘{‘ and ‘}’ and are separated by commas.The syntax is shown below: struct tag_name variable = {v1, v2, ...vn }; • struct employee { char name[20]; int salary; int id; } a = {"Shivashankar", 10950, 2001}; Khanal Pralhad: pkhanal95.pk@gmail.com
  • 11.
    Accessing Structure Member -Themembers of a structure are separate entities and so should be processed individually. -A member of a structure can be accessed by specifying the variable name followed by a period (also called dot) which in turn is followed by the member name using the syntax shown below: variable.member Consider the structure definition and initialization shown below: struct employee { char name[10]; float salary; int id; } struct employee a = {“RAMA", 10950, 2001}; Khanal Pralhad: pkhanal95.pk@gmail.com
  • 12.
    By specifying a.namewe can access the string "RAMA". By specifying a.salary we can access the value 10950.000000 By specifying a.id we can access the value of 2001 printf(“%sn”, a.name); RAMA printf(“%fn”, a.salary); 10950.000000 printf(“%dn”, a.id); 2001 Khanal Pralhad: pkhanal95.pk@gmail.com
  • 13.
    Example struct Student { char stud_name[30]; introll_number; float percentage; } stud_1 ; void main(){ struct Student stud_2; printf("Enter details of stud_1 : n"); printf("Name : "); scanf("%s", stud_1.stud_name); printf("Roll Number : "); scanf("%d", &stud_1.roll_number); printf("Percentage : "); scanf("%f", &stud_1.percentage); printf("Name of the Student : %sn", stud_1.stud_name); printf("Roll Number of the Student : %in", stud_1.roll_number); printf("Percentage of the Student : %fn", stud_1.percentage); } Khanal Pralhad: pkhanal95.pk@gmail.com
  • 14.
    Program :- Toarrange student’s information in ascending order using an array Khanal Pralhad: pkhanal95.pk@gmail.com
  • 15.
    struct student{ char name[25]; intrno; int marks[3]; }; void main(){ struct student s; int i; float total=0,avg; printf("Enter Student Name:"); gets(s.name); printf("Enter Student Rollno:"); scanf("%d",&s.rno); printf("ntttEnter Student Marks Detailsn"); for(i=0;i<3;i++){ printf("Enter Student Subject %d Marks: ",i+1); scanf("%d",&s.marks[i]); total= total+s.marks[i];} avg=total/3; printf("tt Student Detailsn"); printf("Student Name : "); puts(s.name); printf("nStudent Roll no: %dn",s.rno); printf("ttStudent Marksn"); printf("tt------- -----nn"); for(i=0;i<3;i++) printf("ttSubject %d : %dn",i+1,s.marks[i]); printf("tt ====n"); printf("ttTotal : %fn",total); printf("ttAverage : %fn",avg); getch(); } Khanal Pralhad: pkhanal95.pk@gmail.com
  • 16.
  • 17.
    UNION • In Cprogramming language, the union is a collection of elements of the different data type. • The union is used to create user-defined data type in the C programming language. As the union used to create a user-defined data type, the union is also said to be “user-defined data type in C”. • In other words, the union is a collection of non-homogeneous elements. • Using union we can define new data types called user-defined data types that holds multiple values of the different data type. Khanal Pralhad: pkhanal95.pk@gmail.com
  • 18.
    Syntax: Union • Thegeneral format or syntax of a union is shown below: union tag_name { type1 member1; type2 member2; ……… ………………. ……… ………………. } ; Khanal Pralhad: pkhanal95.pk@gmail.com
  • 19.
    Example: Union union Student { charstud_name[30]; int roll_number; float percentage; } ; Khanal Pralhad: pkhanal95.pk@gmail.com
  • 20.
    union Student { char stud_name[30]; introll_number; float percentage; } stud_1 ; // while defining union void main(){ union Student stud_2; // using union keyword printf("Enter details of stud_1 : n"); printf("Name : "); scanf("%s", stud_1.stud_name); printf("Roll Number : "); scanf("%d", &stud_1.roll_number); printf("Percentage : "); scanf("%f", &stud_1.percentage); printf("Name of the Student : %sn", stud_1.stud_name); printf("Roll Number of the Student : %in", stud_1.roll_number); printf("Percentage of the Student : %fn", stud_1.percentage); } Khanal Pralhad: pkhanal95.pk@gmail.com
  • 21.
  • 22.
  • 23.
    • Typedef typedef structBooks { char title[50]; char author[50]; char subject[100]; int book_id; } Book; • Nested structure Khanal Pralhad: pkhanal95.pk@gmail.com
  • 24.
    typedef struct student { introll_no; char name[30]; int phone_number; }st; int main() { st p1, p2, p3; p1.roll_no = 1; strcpy(p1.name,"Brown"); p1.phone_number = 123443; p2.roll_no = 2; strcpy(p2.name,"Sam"); p2.phone_number = 1234567822; p3.roll_no = 3; strcpy(p3.name,"Addy"); p3.phone_number = 1234567844; printf("First Studentn"); printf("roll_no : %dn", p1.roll_no); printf("name : %sn", p1.name); printf("phone_number : %dn", p1.phone_number); printf("Second Studentn"); printf("roll_no : %dn", p2.roll_no); printf("name : %sn", p2.name); printf("phone_number : %dn", p2.phone_number); printf("Third Studentn"); printf("roll_no : %dn", p3.roll_no); printf("name : %sn", p3.name); printf("phone_number : %dn", p3.phone_number); return 0; } Khanal Pralhad: pkhanal95.pk@gmail.com
  • 25.
    Assignments 1. WAP thattakes the address and marks of 10 students .Sort data according to marks in descending order and display. 2. WAP to take stud_roll,fname,lname of 10 students and display them. 3. WAP to take stud_roll,fname,lname of 10 students and display them on the basis of stud_roll number(descending order). Khanal Pralhad: pkhanal95.pk@gmail.com
  • 26.
  • 27.