"Structure and Union": Presented By: Sunil Subedi Roshan Ray Chaudary Ushan Buddhacharya
"Structure and Union": Presented By: Sunil Subedi Roshan Ray Chaudary Ushan Buddhacharya
UNION”
Presented By:
• Sunil Subedi
• Roshan Ray Chaudary
• Ushan Buddhacharya
Let’s Start With
Structure
“STRUCTURE”
Quick Notes
struct student
#include<stdio.h>
#include<conio.h>
struct student //structure prototype and definition
{
char name[25];
char add[25];
}example;
Optional step
void main()
{
struct student example; //where example is a structure variable
clrscr();
printf(“\n Enter name and address : ”);
scanf(“%s%s”,example.name,example.add); //see how value is stored
for(i=0;i<3;i++)
{
printf(“\n Enter students name and address:”)
scanf(“%s%s”,std[i].name,std[i].add);
}
for(i=0;i<3;i++)
{
printf(“\n Your name is %s and you are from %s”,std[i].name, std[i].add);
}
getch();
}
“Output”
Your name is Ram and you are from Thamel
Your name is Hari and you are from Bhaktapur
Your name is Shyam and you are from Ason
#include<stdio.h>
#include<conio.h>
struct student
{
char name[25];
int roll_no;
int marks[5]; //array to store marks in 5 subjects
}a;
void main()
{
int i, total=0;
clrscr();
printf(“\n Enter name and roll number of the student : ”);
scanf(“%s %d”,a.name,&a.roll_no);
//to record marks
printf(“\n Enter the marks in 5 subjects : ”);
for(i=0;i<5;i++)
{
scanf(“%d”,&a.marks[i]); //where the value of i varies in different section of loops
}
for(i=0;i<5;i++)
{
total+=a.marks[i];
}
printf(“\n Name:%s \n Roll No:%d \n Total Marks:%d”,a.name,a.roll_no,total);
getch();
}
“Output”
Name : Ram
Roll No : 7
Total Marks : 85
Where “Ram”, ‘7’ are input for name and roll number
And
((20,15,20,18,12)) are marks input.
“STRUCTURES AND
FUNCTIONS”
The main philosophy of the C language is to use functions.
Examples:
1. struct student
scanf(“%d”,&a.scores.marks); 2. {
printf(“%d”,a.scores.marks); 3. char name[25];
4. char faculty[10];
5. struct
6. {
7. int marks;
8. float per;
9. char grade;
10. }scores; //nested structure tag or
name
11. }a; //main structure tag or name
/*program to demonstrate a simple nested structure program*/
#include<stdio.h>
#include<conio.h>
struct Employee
{
char name[20];
int id;
float salary;
struct Date
{
int date;
int month;
int year;
}doj;
}emp = {“Ram",1000,1000.50,{22,6,1990}};
void main()
{
printf("\nEmployee Name : %s",emp.ename);
printf("\nEmployee ID : %d",emp.id);
printf("\nEmployee Salary : %f",emp.salary);
printf("\nEmployee DOJ : %d/%d/%d", \ emp.doj.date,emp.doj.month,emp.doj.year);
getch();
}
“Output”
Name : Ram
ID : 1000
Salary : 1000.50
DOJ : 22/6/1990
Only one element in the union may have a value set at any given
time.
Values are not assigned to all the members at any one time.
here the size of the structure ‘item’ is equal here the size of the structure ‘item’ is equal to
to the size of its greatest member. the sum of total size of its members.
Union is given in
#include<stdio.h>
#include<conio.h>
union example
{
int i;
float f;
“Output”
char c[20];
};
void main( )
{
union example data;
data.i = 10;
data.f = 220.5; data.i = 1917853763
data.c = “Structure”; data.f = 41223605808.000000
printf( “\n data.i : %d", data.i);
printf( “\n data.f : %f", data.f);
data.c = Structure
printf( “\n data.c : %s", data.c);
getch();
}
Here, we can see that values of i and f members of union got corrupted because final value
assigned to the variable has occupied the memory location and this is the reason that the
value of c member is getting printed very well.
Now let's look into the same example once again where we will use one
variable at a time which is the main purpose of having union:
#include<stdio.h>
#include<conio.h>
union example
“Output”
{
int i;
float f;
char c[20];
};
void main( ) data.i : 10
{
union example data;
data.f : 220.500000
data.i = 10; data.c:Structure
printf( “\n data.i : %d", data.i);
data.f = 220.5;
printf( “\n data.f : %f", data.f);
data.c = “Structure”;
printf( “\n data.c : %s", data.c);
getch();
}
Here, all the members are getting printed very well because one member is being used
at a time.
Thank You…..