Structure is a
user defined data type. A structure is a collection of variables
that is referenced under a single name. A structure contains a number of data
types grouped together. These data types may or may not be of the
same type. Structure occupies total no.of bytes. To know the size of the
structure and accessing the structure variables we need structure object. we
use struct keyword for declaring a structure. Structure always ends with
semicolon. We can declare a structure before a main() or within the main().
Declaring a
structure:-
Syntax:-
struct
<structure name>
{
structure element 1;
structure element 2;
---
structure element n;
};
structure Objects;
we can create
no.of objects for a structure. We can create a structure object before
structure ends with semicolon or in main().
With in the
main() if you want to create a structure object use the following syntax.
<struct>
<structure-name> <structure-object>;
Wap read and
print the employee information with using structures.
#include<stdio.h>
struct sai
{
int eno,sal;
char nm[20];
}s;
main()
{
clrscr();
printf("\nStructure
occupies %d bytes",sizeof(s));
printf("\nEnter
eno,name,salary:");
scanf("%d%s%d",&s.eno,&s.nm,&s.sal);
printf("\nEmployee
details");
printf("\nEmployee
Number is %d",s.eno);
printf("\nEmployee
Name is %s",s.nm);
printf("\nEmployee
Salary is %d",s.sal);
getch();
}
Structures with
Array:- In the structure object if you specify the size is nothing but
structure with array.
Wap read and
print the 3 students information and print with using structures using arrays.
#include<stdio.h>
main()
{
struct student
{
int
sno,s1,s2,s3,tot;
char nm[10];
};
struct student
s[3];
int i;
clrscr();
printf("\nThis
Structure occupies %d bytes",sizeof(s));
for(i=0;i<=2;i++)
{
printf("Enter
student no,name,subject1,subject2,subject3 marks:");
scanf("%d%s%d%d%d",&s[i].sno,&s[i].nm,&s[i].s1,&s[i].s2,&s[i].s3);
}
for(i=0;i<=2;i++)
{
printf("\n--------------------");
printf("\nSTUDENT
INFORMATION");
printf("\n---------------------");
printf("\nStudent
No=%d",s[i].sno);
printf("\nStudent
Name=%s",s[i].nm);
printf("\nStudent
Subject1=%d",s[i].s1);
printf("\nStudent
Subject2=%d",s[i].s2);
printf("\nStudent
Subject3=%d",s[i].s3);
s[i].tot=s[i].s1+s[i].s2+s[i].s3;
printf("\nStudent
Total=%d",s[i].tot);
if(s[i].s1>34
&& s[i].s2>34 && s[i].s3>34)
printf("\nPass");
else
printf("\nFail");
printf("\n----------------------");
}
getch();
}
structure with
Pointers:- In the structure object if you specify the pointer that is
nothing but structure with pointer.
WAP to calculate
the student total with using structure with pointer.
#include<stdio.h>
main()
{
struct samp
{
int
sno,sub1,sub2,sub3,tot;
char n[10];
}s,*p;
p=&s;
printf("Enter
sno,name,sub1,sub2,sub3 marks:");
scanf("%d%s%d%d%d",&p->sno,&p->n,&p->sub1,&p->sub2,&p->sub3);
p->tot=p->sub1+p->sub2+p->sub3;
printf("\nSTUDENT
INFORMATION");
printf("\nSNO
= %d",p->sno);
printf("\nSNAME
= %s",p->n);
printf("\nSubject
1=%d",p->sub1);
printf("\nSubject
2=%d",p->sub2);
printf("\nSubject
3=%d",p->sub3);
printf("\nTotal
=%d",p->tot);
}
Note:- * and . is
replaced with -> operator.
Structure with
functions:-In the function if you pass the structure object as the
parameter that is nothing but structure with functions. For working with
structure with functions structure is before the main() is compulsary. Calling
function doesn’t required. Return type doesn’t required.
Wap read 4
numbers and check those are equal or not.
#include<stdio.h>
struct samp
{
int m,n,o,p;
}s,s1;
main()
{
printf("Enter
4 numbers:");
scanf("%d%d%d%d",&s.m,&s.n,&s1.o,&s1.p);
fun(s,s1);
}
fun(struct samp
u,struct samp u1)
{
if(u.m==u1.o
&& u.n==u1.p)
printf("\nBoth
values are Equal.");
else
printf("\nBoth
values are not Equal.");
}
Structure with
structure:- In the structure if u create another structure is called
structure with structure.
Or
In one structure
if you create object for another structure is called structure with structure.
#include<stdio.h>
main()
{
struct emp
{
int eno;
char n[10];
struct employee
{
int sal;
}s;
}e;
clrscr();
printf("Enter
eno,ename,salary:");
scanf("%d%s%d",&e.eno,&e.n,&e.s.sal);
printf("\nEMPLOYEE
INFORMATION");
printf("\nENO
= %d",e.eno);
printf("\nENAME
= %s",e.n);
printf("\nSalary
=%d",e.s.sal);
getch();
}
Wap to findout
the biggest number among two numbers.
#include<stdio.h>
struct samp
{
int n;
};
struct sample
{
int m;
struct samp s;
}t;
main()
{
clrscr();
printf("Enter
two numbers:");
scanf("%d%d",&t.s.n,&t.m);
if(t.s.n>t.m)
printf("\n%d
is a Big.",t.s.n);
else
printf("\n%d
is a Big.",t.m);
}
No comments:
Post a Comment