Structure
Structure is a collection of variables of different data types grouped together under a single name.
Each variable within a structure is called as member of structure.
Syntax of defining structure :-
struct structure_name
{
Structure_member 1;
Structure_member 2;
...............................
................................
Structure_member n;
}instances;
OR
struct structure_name
{
Structure_member 1;
Structure_member 2;
...............................
................................
Structure_member n;
};
struct structure_name instance;
Ex-
struct student
{
char name[20];
int rollno;
float per;
}stud1,stude2;
OR
struct student
{
char name[20];
int rollno;
float per;
}
struct student stud1,stud2;
Initialization of structure Variable:
An instance of a structure can be assigned values during declaration as follows.
struct student
{
char name[20];
int rollno;
float per;
}stud1={"irawen",101,78.12};
Accessing Structure Member:
Individual members of structure can be used like other variables. Structure member can be assessed by dot operator(.).
This dot operator (.) is used between the structure name and member name.
Ex- stud1.name , stud1.rollno , stud1.per
values can also be assigned to structure member.
stud1.name="irawen";
stud1.rollno=101;
stud1.per=78.12;
Question 1) Write a program to define structure 'Tender' having data member tender_no,cost and company_name.Accept and display data for one variable of the structure.
#include<conio.h>
#include<iostream.h>
struct Tender
{
int tender_no;
float cost;
char company_name[20];
};
void main()
{
Tender t1;
clrscr();
cout<<"\tEnter Tender Number";
cin>>t1.tender_no;
cout<<"\nEnter Cost of Tender";
cin>>t1.cost;
cout<<"\nEnter Company Name";
cin>>t1.company_name;
cout<<"\n\n\tTender Number is →"<<t1.tender_no;
cout<<"\n\n\tTender cost is→"<<t1.cost;
cout<<"\n\n\tCompany Name is→"<<t1.company_name;
getch();
}
Output-
Enter Tender Number101
Enter Cost of Tender1234
Enter Company NameIrawen.info
Tender Number is→101
Tender cost is→1234
Tender Name is→Irawen.info
Union :
Union are a concept borrowed from structures and therefore the same syntax as structure.
But here is a major difference between them is in storage structures. In structure there is a separate storage memory location for each data member where all the member of union share the memory locations.
Ex-
Union item
{
int a;
float b;
char c;
} it;
Example Union-
#include<conio.h>
#include<stdio.h>
void main()
{
union item
{
int m;
float p;
char ch;
}it;
clrscr();
it.m=12;
it.p=23.12;
it.ch='A';
printf("\n\n\tItem No is→%d",it.m);
printf("\n\n\tPrice is→%f",it.p);
printf("\n\n\tStarting character of item is→%c",it.ch);
getch();
}
Output-
Item No is→2751
Price is→23.119753
Starting character of item is→A
Enumerated Data Types:-
An enumerated data type is another user defined type which provides a way of attaching names to the members.
The enum keyword automatically enumerates the list of words by assigning values 0,1,2.....and so on.
Syntax:
enum shape{circle.square,triangle};
enum color{red,blue,green,yellow};
:Declaring Variable of enum:
Ex.shape ellipse;color background;
By default enum assigns integer values starting with '0' zero for the first enumerator,1 for the second and so on..
We can over ride the default by just assigning integer values to enumerator.
enum color{red,blue=4,green=3};
enum color{red=5,blue,green};
#include<iostream.h>
#include<conio.h>
void main();
{
enum color{red,blue,green};
enum color1{red1,blue1=4,green1=6};
cout<<"\n\n\tColor of Background-->"<<red;
cout<<"\n\n\tColor of Background-->"<<blue;
cout<<"\n\n\tColor of Background-->"<<green;
cout<<"\n\n\tColor of Background-->"<<red1;
cout<<"\n\n\tColor of Background-->"<<blue1;
cout<<"\n\n\tColor of Background-->"<<green1;
getch();
}
Output-
Color of Background-->0
Color of Background-->1
Color of Background-->2
Color of Background-->0
Color of Background-->4
Color of Background-->6
Structure is a collection of variables of different data types grouped together under a single name.
Each variable within a structure is called as member of structure.
Syntax of defining structure :-
struct structure_name
{
Structure_member 1;
Structure_member 2;
...............................
................................
Structure_member n;
}instances;
OR
struct structure_name
{
Structure_member 1;
Structure_member 2;
...............................
................................
Structure_member n;
};
struct structure_name instance;
Ex-
struct student
{
char name[20];
int rollno;
float per;
}stud1,stude2;
OR
struct student
{
char name[20];
int rollno;
float per;
}
struct student stud1,stud2;
Initialization of structure Variable:
An instance of a structure can be assigned values during declaration as follows.
struct student
{
char name[20];
int rollno;
float per;
}stud1={"irawen",101,78.12};
Accessing Structure Member:
Individual members of structure can be used like other variables. Structure member can be assessed by dot operator(.).
This dot operator (.) is used between the structure name and member name.
Ex- stud1.name , stud1.rollno , stud1.per
values can also be assigned to structure member.
stud1.name="irawen";
stud1.rollno=101;
stud1.per=78.12;
Question 1) Write a program to define structure 'Tender' having data member tender_no,cost and company_name.Accept and display data for one variable of the structure.
#include<conio.h>
#include<iostream.h>
struct Tender
{
int tender_no;
float cost;
char company_name[20];
};
void main()
{
Tender t1;
clrscr();
cout<<"\tEnter Tender Number";
cin>>t1.tender_no;
cout<<"\nEnter Cost of Tender";
cin>>t1.cost;
cout<<"\nEnter Company Name";
cin>>t1.company_name;
cout<<"\n\n\tTender Number is →"<<t1.tender_no;
cout<<"\n\n\tTender cost is→"<<t1.cost;
cout<<"\n\n\tCompany Name is→"<<t1.company_name;
getch();
}
Output-
Enter Tender Number101
Enter Cost of Tender1234
Enter Company NameIrawen.info
Tender Number is→101
Tender cost is→1234
Tender Name is→Irawen.info
Union :
Union are a concept borrowed from structures and therefore the same syntax as structure.
But here is a major difference between them is in storage structures. In structure there is a separate storage memory location for each data member where all the member of union share the memory locations.
Ex-
Union item
{
int a;
float b;
char c;
} it;
Example Union-
#include<conio.h>
#include<stdio.h>
void main()
{
union item
{
int m;
float p;
char ch;
}it;
clrscr();
it.m=12;
it.p=23.12;
it.ch='A';
printf("\n\n\tItem No is→%d",it.m);
printf("\n\n\tPrice is→%f",it.p);
printf("\n\n\tStarting character of item is→%c",it.ch);
getch();
}
Output-
Item No is→2751
Price is→23.119753
Starting character of item is→A
Enumerated Data Types:-
An enumerated data type is another user defined type which provides a way of attaching names to the members.
The enum keyword automatically enumerates the list of words by assigning values 0,1,2.....and so on.
Syntax:
enum shape{circle.square,triangle};
enum color{red,blue,green,yellow};
:Declaring Variable of enum:
Ex.shape ellipse;color background;
By default enum assigns integer values starting with '0' zero for the first enumerator,1 for the second and so on..
We can over ride the default by just assigning integer values to enumerator.
enum color{red,blue=4,green=3};
enum color{red=5,blue,green};
#include<iostream.h>
#include<conio.h>
void main();
{
enum color{red,blue,green};
enum color1{red1,blue1=4,green1=6};
cout<<"\n\n\tColor of Background-->"<<red;
cout<<"\n\n\tColor of Background-->"<<blue;
cout<<"\n\n\tColor of Background-->"<<green;
cout<<"\n\n\tColor of Background-->"<<red1;
cout<<"\n\n\tColor of Background-->"<<blue1;
cout<<"\n\n\tColor of Background-->"<<green1;
getch();
}
Output-
Color of Background-->0
Color of Background-->1
Color of Background-->2
Color of Background-->0
Color of Background-->4
Color of Background-->6
0 Comments:
Post a Comment