Structure In C.
structure is kind of grouping ,if we want to group different element we use structure
for example we use it if we want to group int and char datatype.
because we know that array is used to store same data type only.
if we want to store the student info like name,roll no,mark.so hear we use char and int and float datatype.
so it benificial for us to see that record in one place like in sql,so for that purpose we use structures.
how to create structure
struct name_of_structure
{
char name[30];
int roll;
float mark;
}s;
so hear what is 's'??
s is a structure created by above 5 line code which have three attribute
name,roll,mark.
below is basic code of structure
===================================================================================
#include <stdio.h>
struct student{
char name[10];
int roll;
float mark;
}s;
int main()
{
int t=0;
while(t<2){
printf("enter info\n");
printf("enter name:\n");
scanf("%s",s.name);
printf("enter rollno:\n");
scanf("%d",&s.roll);
printf("enter mark:\n");
scanf("%f",&s.mark);
printf("display data\n");
printf("name:%s\n",s.name);
printf("roll:%d\n",s.roll);
printf("mark:%.2f\n",s.mark);
t++;
}
return 0;
}
=====================================================================
output
enter info
enter name:rohan
enter rollno:20
enter mark:100
display data
name:rohan
roll:20
mark:100.00
enter info
enter name:abc
enter rollno:10
enter mark:400
display data
name:abc
roll:10
mark:400.00
====================================================================
Addition using structure.
#include<stdio.h>
struct num
{
int a,b;
float c;
}d1,d2,result;
int main()
{
printf("enter first number:");
scanf("%d",&d1.a);
printf("enter second number:");
scanf("%d",&d2.b);
printf("sum is");
result.c = d1.a + d2.b;
printf("%2.f",result.c);
}
====================================================================
enter first number:5
enter second number:4
sum is 9 ===================================================================
Comments
Post a Comment